unity将物体 旋转指定角度


要实现这个功能,需要用四元数。你不用知道什么叫四元数,你只需要知道怎么用就行了。

例如每按一次空格,让一个物体绕Y轴旋转90度

 public float agle=90;
 private int n = 0;
 public float speed = 5;
// Update is called once per frame
void Update () {
     if (Input.GetKeyDown(KeyCode.Space))
     {
          ++n ;
      }
      transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(Vector3.up * (90 * n % 360)), Time.deltaTime * speed);
}

当然也可以使用
 Quaternion.Slerp(r1,r2,speed);
两者的区别你可以在API中查看。

第二篇修改后
void Update () {
        //设置物体的旋转为指定角度 (0-360)度 0-360这里是角度不是弧度
        //弧度与角度的关系是 弧度=弧长/半径
        transform.rotation = Quaternion.Euler(Vector3.up * (yaw % 360));
        if (Input.GetKeyDown(KeyCode.A))
        {
            n++;
        }
        //按一次键盘 物体旋转90度
        transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(Vector3.up * (90 * n % 360)), Time.deltaTime * speed);
}

 

end

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM