YanLingLookAtCamera.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// 言灵朝向相机
  6. /// </summary>
  7. public class YanLingLookAtCamera : MonoBehaviour
  8. {
  9. private Camera mMainCamera = null;
  10. private Vector3 pos = Vector3.zero;
  11. // Start is called before the first frame update
  12. void Start()
  13. {
  14. mMainCamera = Camera.main;
  15. transform.localPosition = new Vector3(0, 2.5f, 0);
  16. }
  17. // Update is called once per frame
  18. void Update()
  19. {
  20. mMainCamera = Camera.main;
  21. Transform target = mMainCamera.transform;
  22. //if (mMainCamera != null)
  23. //{
  24. // pos.x = mMainCamera.transform.position.x;
  25. // pos.y = mTransform.position.y;
  26. // pos.z = mMainCamera.transform.position.z;
  27. // mTransform.LookAt(pos);
  28. //}
  29. var forward = target.TransformDirection(Vector3.forward);
  30. //transform.position = target.position + forward * distance;
  31. var toward = Quaternion.identity;
  32. toward.SetLookRotation(-target.forward, target.up);
  33. transform.rotation = toward;
  34. }
  35. }