慢慢(每次一度)转向目标

来源:互联网 发布:泗洪网络问政 编辑:程序博客网 时间:2024/04/29 10:01

一道事后做出来的面试题

效果:实现每次点击rotation3个值改变一度,最终实现朝向目标的结果.

主要是Quaternion.LookRotation(差值向量)这个方法,可以直接得到旋转后角度;

using UnityEngine;using System.Collections;public class loadpic : MonoBehaviour {    public GameObject target;    public Vector3 lerp;    public float x = 0,y = 0,z = 0;    void Start()    {        lerp = target.transform.position - transform.position;    }    void Update()    {        //直接得到旋转后效果        //print(Quaternion.LookRotation(lerp).eulerAngles);        //transform.rotation = Quaternion.LookRotation(lerp);        //慢慢旋转        if (Input.GetMouseButtonDown(0))        {            if (x < Quaternion.LookRotation(lerp).eulerAngles.x)            {                x++;            }            if (y < Quaternion.LookRotation(lerp).eulerAngles.y)            {                y++;            }            if (z < Quaternion.LookRotation(lerp).eulerAngles.z)            {                z++;            }            Quaternion a = new Quaternion(); ;            a.eulerAngles = new Vector3(x, y, z);            transform.rotation = a;        }    }}

有问题,会绕大圈旋转到目标

0 0