美文网首页
unity 类似Lookat缓慢的转动摄像机朝向某个物体

unity 类似Lookat缓慢的转动摄像机朝向某个物体

作者: 吉凶以情迁 | 来源:发表于2022-07-22 17:40 被阅读0次

我尝试了各种写法发现都不对,从网上找到了正确的写法,要转向这个物体 ,是距离相减,然后
Quaternion.LookRotation 然后四元素 转换为vector3,.DOTWEEN是一个封装框架,也可以自己用差值器实现。

下面是我自己写的差值器调用
//参数1 为相机,参数2 为物体 ,
    public static Coroutine postRotateLookAt(Transform transform1, Transform transform2, float execTime)
        {
            {
                TaskObj taskObj = new TaskObj();
                taskObj.execFrame = 0.01f;//多少秒执行一次逻辑
                taskObj.needTime = execTime;
                taskObj.action = delegate ()
                {

                    Vector3 dir = transform2.transform.position - transform1.position;
                    Quaternion rot = Quaternion.LookRotation(dir);
                    transform1.rotation = Quaternion.Lerp(transform2.rotation, rot, taskObj.currentTime * taskObj.needTime);
                };
             return    GlobalClick.getInstance().startTask(taskObj);

            }
                                                       
        }
        public class TaskObj
        {
            public Action action;
            public float needTime;
            public float currentTime;
            public Transform transform1;
            public float execFrame = 0.1f;
            public Transform transform2;
        }


    public IEnumerator loopExec(TaskObj taskObj)
    {

        while (taskObj.currentTime <taskObj.needTime)
        {
            taskObj.action.Invoke();
            yield return new WaitForSeconds(taskObj.execFrame);
            taskObj.currentTime += taskObj.execFrame;
        }
 
        yield return null;
    }
    public Coroutine startTask(TaskObj taskObj)
    {
    return     StartCoroutine(loopExec(taskObj));
    }


上面的时间差参数 写的有点问题

public class TestLookAt : MonoBehaviour
{
    // Start is called before the first frame update
public GameObject gameObject1;
public GameObject gameObject2;
    void Start()
    {
        postRotateLookAt(gameObject1.transform, gameObject2.transform,3);
    }

    // Update is called once per frame
    void Update()
    {


    }



    public IEnumerator loopExec(TaskObj taskObj)
    {

        while (taskObj.currentTime < taskObj.needTime)
        {
            taskObj.action.Invoke();
            print("调用" + taskObj.currentTime);
            yield return new WaitForSeconds(taskObj.execFrame);
            taskObj.currentTime += taskObj.execFrame;
            print("调用over" + taskObj.currentTime);
        }
        print("结束");

        yield return null;
    }
    public Coroutine startTaskTest(TaskObj taskObj)
    {
        return StartCoroutine(loopExec(taskObj));
    }



    public  Coroutine postRotateLookAt(Transform transform1, Transform transform2, float execTime)
    {
        {
            TaskObj taskObj = new TaskObj();
            taskObj.execFrame = 0.01f;//多少秒执行一次逻辑
            taskObj.needTime = execTime;
            taskObj.action = delegate ()
            {

                Vector3 dir = transform2.transform.position - transform1.position;
                Quaternion rot = Quaternion.LookRotation(dir);
                float times = taskObj.currentTime / taskObj.needTime*1.0f;
                print("times" + times);
                transform1.rotation = Quaternion.Lerp(transform1.rotation, rot, times);
            };
            return startTaskTest(taskObj);

        }

    }
}

第三方实现DOTWEEN


            Vector3  dir = gameObject.transform.position - Camera.main.transform.position;
            Quaternion rot = Quaternion.LookRotation(dir);
            Camera.main.transform.DORotate(new Vector3(rot.eulerAngles.x, rot.eulerAngles.y, rot.eulerAngles.z), 1.5f, RotateMode.Fast);

相关文章

网友评论

      本文标题:unity 类似Lookat缓慢的转动摄像机朝向某个物体

      本文链接:https://www.haomeiwen.com/subject/ykamirtx.html