当前位置:   article > 正文

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

unity 摄像机看向物体

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

  1. 下面是我自己写的差值器调用
  2. //参数1 为相机,参数2 为物体 ,
  3. public static Coroutine postRotateLookAt(Transform transform1, Transform transform2, float execTime)
  4. {
  5. {
  6. TaskObj taskObj = new TaskObj();
  7. taskObj.execFrame = 0.01f;//多少秒执行一次逻辑
  8. taskObj.needTime = execTime;
  9. taskObj.action = delegate ()
  10. {
  11. Vector3 dir = transform2.transform.position - transform1.position;
  12. Quaternion rot = Quaternion.LookRotation(dir);
  13. transform1.rotation = Quaternion.Lerp(transform2.rotation, rot, taskObj.currentTime * taskObj.needTime);
  14. };
  15. return GlobalClick.getInstance().startTask(taskObj);
  16. }
  17. }
  18. public class TaskObj
  19. {
  20. public Action action;
  21. public float needTime;
  22. public float currentTime;
  23. public Transform transform1;
  24. public float execFrame = 0.1f;
  25. public Transform transform2;
  26. }
  27. public IEnumerator loopExec(TaskObj taskObj)
  28. {
  29. while (taskObj.currentTime <taskObj.needTime)
  30. {
  31. taskObj.action.Invoke();
  32. yield return new WaitForSeconds(taskObj.execFrame);
  33. taskObj.currentTime += taskObj.execFrame;
  34. }
  35. yield return null;
  36. }
  37. public Coroutine startTask(TaskObj taskObj)
  38. {
  39. return StartCoroutine(loopExec(taskObj));
  40. }

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

  1. public class TestLookAt : MonoBehaviour
  2. {
  3. // Start is called before the first frame update
  4. public GameObject gameObject1;
  5. public GameObject gameObject2;
  6. void Start()
  7. {
  8. postRotateLookAt(gameObject1.transform, gameObject2.transform,3);
  9. }
  10. // Update is called once per frame
  11. void Update()
  12. {
  13. }
  14. public IEnumerator loopExec(TaskObj taskObj)
  15. {
  16. while (taskObj.currentTime < taskObj.needTime)
  17. {
  18. taskObj.action.Invoke();
  19. print("调用" + taskObj.currentTime);
  20. yield return new WaitForSeconds(taskObj.execFrame);
  21. taskObj.currentTime += taskObj.execFrame;
  22. print("调用over" + taskObj.currentTime);
  23. }
  24. print("结束");
  25. yield return null;
  26. }
  27. public Coroutine startTaskTest(TaskObj taskObj)
  28. {
  29. return StartCoroutine(loopExec(taskObj));
  30. }
  31. public Coroutine postRotateLookAt(Transform transform1, Transform transform2, float execTime)
  32. {
  33. {
  34. TaskObj taskObj = new TaskObj();
  35. taskObj.execFrame = 0.01f;//多少秒执行一次逻辑
  36. taskObj.needTime = execTime;
  37. taskObj.action = delegate ()
  38. {
  39. Vector3 dir = transform2.transform.position - transform1.position;
  40. Quaternion rot = Quaternion.LookRotation(dir);
  41. float times = taskObj.currentTime / taskObj.needTime*1.0f;
  42. print("times" + times);
  43. transform1.rotation = Quaternion.Lerp(transform1.rotation, rot, times);
  44. };
  45. return startTaskTest(taskObj);
  46. }
  47. }
  48. }

第三方实现DOTWEEN

  1. Vector3 dir = gameObject.transform.position - Camera.main.transform.position;
  2. Quaternion rot = Quaternion.LookRotation(dir);
  3. Camera.main.transform.DORotate(new Vector3(rot.eulerAngles.x, rot.eulerAngles.y, rot.eulerAngles.z), 1.5f, RotateMode.Fast);
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/108837
推荐阅读
相关标签
  

闽ICP备14008679号