unity 实现围绕星球走动

来源:互联网 发布:usb网络共享电脑驱动xp 编辑:程序博客网 时间:2024/04/27 21:15

一、效果图



当然如果是规则的圆使用RotateAround可以轻松实现,但是如果这个球有凹凸部分RotateAround实现起来就会有问题了,但是下面的方法应对有凹凸情况依然可以轻松应付。


二、实现基本原理和代码

1、原理通过小球检测向右前方发送射线,给小方块施加右前方的力,同时检测小方块射线检测当前到球的碰撞位置进行旋转。
最后再给方块一个力让它球重心下落。
2、代码
void Update () {        GetComponent<Rigidbody> ().AddForce (transform.right * 50);  //此处会一直加速,速度过快会离心,所以我在属性面板中把阻力也设置为了10        RaycastHit hitForward;        //此处射线检测要排除掉自身,检测到的应该是围绕旋转的中心球        if(Physics.Raycast (transform.position , -transform.up , out hitForward , 10 , RayCastLayerMask)) {            //开启此处会使旋转过渡会平滑点            // transform.rotation = Quaternion.Lerp (transform.rotation , Quaternion.LookRotation (Vector3.Cross (transform.right , hitForward.normal) , hitForward.normal) , Time.deltaTime * 10);            transform.rotation = Quaternion.LookRotation (Vector3.Cross (transform.right , hitForward.normal) , hitForward.normal);        }        //gravity        GetComponent<Rigidbody> ().AddForce (-transform.up * 10);    }

加q群一块学习115136489 demo下载地址1 demo下载2

0 0
原创粉丝点击