CharacterMovement.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using UnityEngine;
  2. namespace Cinemachine.Examples
  3. {
  4. [AddComponentMenu("")] // Don't display in add component menu
  5. public class CharacterMovement : MonoBehaviour
  6. {
  7. public bool useCharacterForward = false;
  8. public bool lockToCameraForward = false;
  9. public float turnSpeed = 10f;
  10. public KeyCode sprintJoystick = KeyCode.JoystickButton2;
  11. public KeyCode sprintKeyboard = KeyCode.Space;
  12. private float turnSpeedMultiplier;
  13. private float speed = 0f;
  14. private float direction = 0f;
  15. private bool isSprinting = false;
  16. private Animator anim;
  17. private Vector3 targetDirection;
  18. private Vector2 input;
  19. private Quaternion freeRotation;
  20. private Camera mainCamera;
  21. private float velocity;
  22. // Use this for initialization
  23. void Start ()
  24. {
  25. anim = GetComponent<Animator>();
  26. mainCamera = Camera.main;
  27. }
  28. // Update is called once per frame
  29. void FixedUpdate ()
  30. {
  31. #if ENABLE_LEGACY_INPUT_MANAGER
  32. input.x = Input.GetAxis("Horizontal");
  33. input.y = Input.GetAxis("Vertical");
  34. // set speed to both vertical and horizontal inputs
  35. if (useCharacterForward)
  36. speed = Mathf.Abs(input.x) + input.y;
  37. else
  38. speed = Mathf.Abs(input.x) + Mathf.Abs(input.y);
  39. speed = Mathf.Clamp(speed, 0f, 1f);
  40. speed = Mathf.SmoothDamp(anim.GetFloat("Speed"), speed, ref velocity, 0.1f);
  41. anim.SetFloat("Speed", speed);
  42. if (input.y < 0f && useCharacterForward)
  43. direction = input.y;
  44. else
  45. direction = 0f;
  46. anim.SetFloat("Direction", direction);
  47. // set sprinting
  48. isSprinting = ((Input.GetKey(sprintJoystick) || Input.GetKey(sprintKeyboard)) && input != Vector2.zero && direction >= 0f);
  49. anim.SetBool("isSprinting", isSprinting);
  50. // Update target direction relative to the camera view (or not if the Keep Direction option is checked)
  51. UpdateTargetDirection();
  52. if (input != Vector2.zero && targetDirection.magnitude > 0.1f)
  53. {
  54. Vector3 lookDirection = targetDirection.normalized;
  55. freeRotation = Quaternion.LookRotation(lookDirection, transform.up);
  56. var diferenceRotation = freeRotation.eulerAngles.y - transform.eulerAngles.y;
  57. var eulerY = transform.eulerAngles.y;
  58. if (diferenceRotation < 0 || diferenceRotation > 0) eulerY = freeRotation.eulerAngles.y;
  59. var euler = new Vector3(0, eulerY, 0);
  60. transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(euler), turnSpeed * turnSpeedMultiplier * Time.deltaTime);
  61. }
  62. #else
  63. InputSystemHelper.EnableBackendsWarningMessage();
  64. #endif
  65. }
  66. public virtual void UpdateTargetDirection()
  67. {
  68. if (!useCharacterForward)
  69. {
  70. turnSpeedMultiplier = 1f;
  71. var forward = mainCamera.transform.TransformDirection(Vector3.forward);
  72. forward.y = 0;
  73. //get the right-facing direction of the referenceTransform
  74. var right = mainCamera.transform.TransformDirection(Vector3.right);
  75. // determine the direction the player will face based on input and the referenceTransform's right and forward directions
  76. targetDirection = input.x * right + input.y * forward;
  77. }
  78. else
  79. {
  80. turnSpeedMultiplier = 0.2f;
  81. var forward = transform.TransformDirection(Vector3.forward);
  82. forward.y = 0;
  83. //get the right-facing direction of the referenceTransform
  84. var right = transform.TransformDirection(Vector3.right);
  85. targetDirection = input.x * right + Mathf.Abs(input.y) * forward;
  86. }
  87. }
  88. }
  89. }