CharacterMovement.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. input.x = Input.GetAxis("Horizontal");
  32. input.y = Input.GetAxis("Vertical");
  33. // set speed to both vertical and horizontal inputs
  34. if (useCharacterForward)
  35. speed = Mathf.Abs(input.x) + input.y;
  36. else
  37. speed = Mathf.Abs(input.x) + Mathf.Abs(input.y);
  38. speed = Mathf.Clamp(speed, 0f, 1f);
  39. speed = Mathf.SmoothDamp(anim.GetFloat("Speed"), speed, ref velocity, 0.1f);
  40. anim.SetFloat("Speed", speed);
  41. if (input.y < 0f && useCharacterForward)
  42. direction = input.y;
  43. else
  44. direction = 0f;
  45. anim.SetFloat("Direction", direction);
  46. // set sprinting
  47. isSprinting = ((Input.GetKey(sprintJoystick) || Input.GetKey(sprintKeyboard)) && input != Vector2.zero && direction >= 0f);
  48. anim.SetBool("isSprinting", isSprinting);
  49. // Update target direction relative to the camera view (or not if the Keep Direction option is checked)
  50. UpdateTargetDirection();
  51. if (input != Vector2.zero && targetDirection.magnitude > 0.1f)
  52. {
  53. Vector3 lookDirection = targetDirection.normalized;
  54. freeRotation = Quaternion.LookRotation(lookDirection, transform.up);
  55. var diferenceRotation = freeRotation.eulerAngles.y - transform.eulerAngles.y;
  56. var eulerY = transform.eulerAngles.y;
  57. if (diferenceRotation < 0 || diferenceRotation > 0) eulerY = freeRotation.eulerAngles.y;
  58. var euler = new Vector3(0, eulerY, 0);
  59. transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(euler), turnSpeed * turnSpeedMultiplier * Time.deltaTime);
  60. }
  61. }
  62. public virtual void UpdateTargetDirection()
  63. {
  64. if (!useCharacterForward)
  65. {
  66. turnSpeedMultiplier = 1f;
  67. var forward = mainCamera.transform.TransformDirection(Vector3.forward);
  68. forward.y = 0;
  69. //get the right-facing direction of the referenceTransform
  70. var right = mainCamera.transform.TransformDirection(Vector3.right);
  71. // determine the direction the player will face based on input and the referenceTransform's right and forward directions
  72. targetDirection = input.x * right + input.y * forward;
  73. }
  74. else
  75. {
  76. turnSpeedMultiplier = 0.2f;
  77. var forward = transform.TransformDirection(Vector3.forward);
  78. forward.y = 0;
  79. //get the right-facing direction of the referenceTransform
  80. var right = transform.TransformDirection(Vector3.right);
  81. targetDirection = input.x * right + Mathf.Abs(input.y) * forward;
  82. }
  83. }
  84. }
  85. }