加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

unity rotate旋转方向角度修正

(2016-07-20 14:40:15)
非第一人称模式下,任务操作WASD,W人物超上冰向上走,A人物转向左并超左走

先获取到要转向的方向的角度向量(h=input,getAxis("horezontal"),v=.......)

  Vector3 tagetDir = new Vector3(h, 0f, v);
            Vector3 nowDir = transform.forward;
            float angle = Vector3.Angle(nowDir, tagetDir);//获取夹角

rotation函数沿着vector3.up旋转时,只能顺时针,为了避免出现需要逆时针旋转小度数顺时针旋转一个大的度数,对所有逆时针旋转式进行修正

当人物左转时,如果面朝上,就让得到的角度为负,其他类推。

                //为了避免转90°变为转270°做出修正

                if (Input.GetKey(KeyCode.A) && (transform.localRotation.eulerAngles.y < 90 || transform.localRotation.eulerAngles.y > 270))
                {
                    angle = -angle;
                }
                else if (Input.GetKey(KeyCode.S) && transform.localRotation.eulerAngles.y > 180)
                {
                    angle = -angle;
                }
                else if (Input.GetKey(KeyCode.D) && (transform.localRotation.eulerAngles.y < 270 && transform.localRotation.eulerAngles.y > 90))
                {
                    angle = -angle;
                }
                else if (Input.GetKey(KeyCode.W) && transform.localRotation.eulerAngles.y < 180)
                {
                    angle = -angle;
                }
            transform.Rotate(Vector3.up * angle * Time.deltaTime * rotateSpeed);


0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有