AvatarController.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using UnityEngine;
  2. namespace TriLibCore.Samples
  3. {
  4. /// <summary>Represents a class used to control an avatar on TriLib samples.</summary>
  5. public class AvatarController : AbstractInputSystem
  6. {
  7. /// <summary>The Avatar Controller Singleton instance.</summary>
  8. public static AvatarController Instance { get; private set; }
  9. /// <summary>
  10. /// Maximum avatar speed in units/second.
  11. /// </summary>
  12. private const float MaxSpeed = 2f;
  13. /// <summary>
  14. /// Avatar acceleration in units/second.
  15. /// </summary>
  16. private const float Acceleration = 5f;
  17. /// <summary>
  18. /// Avatar Friction in units/second.
  19. /// </summary>
  20. private const float Friction = 2f;
  21. /// <summary>
  22. /// Avatar smooth rotation factor.
  23. /// </summary>
  24. private const float RotationSpeed = 60f;
  25. /// <summary>
  26. /// Avatar character controller.
  27. /// </summary>
  28. public CharacterController CharacterController;
  29. /// <summary>
  30. /// Avatar animator.
  31. /// </summary>
  32. public Animator Animator;
  33. /// <summary>
  34. /// Game object that wraps the actual avatar.
  35. /// </summary>
  36. public GameObject InnerAvatar;
  37. /// <summary>
  38. /// Camera offset relative to the avatar.
  39. /// </summary>
  40. private Vector3 _cameraOffset;
  41. /// <summary>
  42. /// Current avatar speed.
  43. /// </summary>
  44. private float _speed;
  45. /// <summary>
  46. /// Camera height offset relative to the avatar.
  47. /// </summary>
  48. private Vector3 _cameraHeightOffset;
  49. /// <summary>
  50. /// Current smooth rotation velocity.
  51. /// </summary>
  52. private float _currentVelocity;
  53. /// <summary>Configures this instance and calculates the Camera offsets.</summary>
  54. private void Awake()
  55. {
  56. Instance = this;
  57. _cameraHeightOffset = new Vector3(0f, CharacterController.height * 0.8f, 0f);
  58. _cameraOffset = Camera.main.transform.position - transform.position;
  59. }
  60. /// <summary>Handles input (controls the Camera and moves the Avatar character).</summary>
  61. private void Update()
  62. {
  63. var input = new Vector3(GetAxis("Horizontal"), 0f, GetAxis("Vertical"));
  64. var direction = Camera.main.transform.TransformDirection(input);
  65. direction.y = 0f;
  66. direction.Normalize();
  67. var targetEulerAngles = direction.magnitude > 0 ? Quaternion.LookRotation(direction).eulerAngles : transform.rotation.eulerAngles;
  68. var eulerAngles = transform.rotation.eulerAngles;
  69. eulerAngles.y = Mathf.SmoothDampAngle(eulerAngles.y, targetEulerAngles.y, ref _currentVelocity, Time.deltaTime * RotationSpeed * input.magnitude);
  70. transform.rotation = Quaternion.Euler(eulerAngles);
  71. _speed += input.magnitude * (Acceleration * MaxSpeed) * Time.deltaTime;
  72. _speed -= Friction * MaxSpeed * Time.deltaTime;
  73. _speed = Mathf.Clamp(_speed, 0f, MaxSpeed);
  74. CharacterController.SimpleMove(transform.forward * _speed);
  75. Animator.SetFloat("SpeedFactor", _speed / MaxSpeed);
  76. var pivotedPosition = Quaternion.AngleAxis(AssetViewerBase.Instance.CameraAngle.x, Vector3.up) * Quaternion.AngleAxis(-AssetViewerBase.Instance.CameraAngle.y, Vector3.right) * _cameraOffset;
  77. Camera.main.transform.position = transform.position + _cameraHeightOffset + pivotedPosition;
  78. Camera.main.transform.LookAt(transform.position + _cameraHeightOffset);
  79. }
  80. }
  81. }