Raycasting光线投射-创新互联

游戏开发人员可以使用光线投射,用于像瞄准,确定视线,测量距离之类的动作。Unity当中的Raycast的重载有很多。现在展示最常用的2中方法

和龙ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:028-86922220(备注:SSL证书合作)期待与您的合作!
bool Raycast( Vector3 origin , Vector3 direction , float distance ,LayerMask mask );

参数解释
 origin : 是光线的开始位置
 direction : 光线的方向
 distance : 光线行进的距离(可为null)
 mask : 确定光线会撞上哪一层(可为null)

如果要确定摄像机前确定是否有某个物体,可以用如下代码

void Update(){
    if (Physics.Raycast(transform.position , transform.forward , 10 )){
        print("there is something in front of the camera!");
    }
}

注意此代码放在camera中

另一个

bool Raycast( Vector3 origin , Vector3 direction , out RaycastHit hit , float distance );

这个重载方法使用了一个RaycastHit类型的参数 , 它是光线碰撞到的对象

void Update(){
    float dirX = Input.GetAxis("Mouse X");
    float dirY = Input.GetAxis("Mouse Y");
    transform.Rotate(dirY , -dirX , 0 );
    UptateRaycastHit();
}
void UpdateRaycastHit(){
    RayCastHit hit;
    if( Physics.Raycast( transform.position , transform.forward , out hit ) ){
        Distroy( hit.collider.gameObject )
    }
}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享题目:Raycasting光线投射-创新互联
网站网址:http://scyanting.com/article/dohich.html