1.单个物体的自旋转
用到的是欧拉角,x、y、z角代表绕z轴旋转z度,绕x轴旋转x度,绕y轴旋转y度(这个顺序)。仅使用这个变量读取和设置角度为绝对值。不要递增它们,当超过角度360度,它将错误。使用Transform.Rotate替代(摘自圣典百科)。
using System.Collections; using System.Collections.Generic; using UnityEngine; public class NewBehaviourScript : MonoBehaviour { // Use this for initialization void Start () { } float Zrotation = 0; // Update is called once per framevoid Update () { transform.eulerAngles = new Vector3(0, 0, Zrotation++); } }写好脚本后,把它拉倒capsule上,其自转效果就产生啦。
2.一个物体绕另一个物体旋转
方法一:让capsule围着cube转
使用public void RotateAround(Vector3 point, Vector3 axis, float angle);
point::围绕的点axiw::围绕的轴ange:旋转的角度
using System.Collections; using System.Collections.Generic; using UnityEngine; public class gongzhuan : MonoBehaviour { public GameObject cubObj; public GameObject capsuleObj; //创建两个对象 // Use this for initialization void Start () { } // Update is called once per frame void Update () { capsuleObj.transform.RotateAround(cubObj.transform.position, Vector3.up, 30 * Time.deltaTime);//让capsule围着cube转 }}这里的所创建的两个对象就是作为unity中的接口,把脚本随便拖到(测试了)cube或者capsule上然后可以在inspect窗口中发现出现如图矩形框中所示的什么什么。。。然后把两个对象相应的拖进椭圆框去就好了(这里已经拖好了),运行一下发现得到了我们想要的结果。
方法二:把cube和capsule作为一个父子关系(拖进去就好啦),如图
然后把自转的脚本挂到cube(父)上就可以啦,不过方位可能要稍稍的改变一下
啊,好累!!
新闻热点
疑难解答