TransformRotation.cs 864 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using UnityEngine;
  2. /// <summary>
  3. /// </summary>
  4. public class TransformRotation : MonoBehaviour
  5. {
  6. public enum RotationType
  7. {
  8. One, Two, Three
  9. }
  10. public RotationType rotationType = RotationType.Three;
  11. private Transform cam;
  12. private Transform bb;
  13. [Range(0, 1)]
  14. public float alignYAxis = 1;
  15. public void Awake()
  16. {
  17. cam = Camera.main.transform;
  18. bb = this.transform;
  19. }
  20. public void Update()
  21. {
  22. if (rotationType == RotationType.One)
  23. {
  24. bb.LookAt(cam);
  25. }
  26. else if (rotationType == RotationType.Two)
  27. {
  28. bb.forward = (cam.position - bb.position).normalized;
  29. }
  30. else
  31. {
  32. bb.rotation = cam.transform.rotation;
  33. }
  34. var f = bb.forward;
  35. f.y *= alignYAxis;
  36. bb.forward = f;
  37. }
  38. }