PlayerControllerRigidBody.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class PlayerControllerRigidBody : MonoBehaviour {
  5. private string MoveInputAxis = "Vertical";
  6. private string TurnInputAxis = "Horizontal";
  7. // rotation that occurs in angles per second holding down input
  8. public float rotationRate = 360;
  9. // units moved per second holding down move input
  10. public float moveRate = 10;
  11. private Rigidbody rb;
  12. private void Start()
  13. {
  14. rb = GetComponent<Rigidbody>();
  15. }
  16. // Update is called once per frame
  17. private void Update ()
  18. {
  19. float moveAxis = Input.GetAxis(MoveInputAxis);
  20. float turnAxis = Input.GetAxis(TurnInputAxis);
  21. ApplyInput(moveAxis, turnAxis);
  22. }
  23. private void ApplyInput(float moveInput,
  24. float turnInput)
  25. {
  26. Move(moveInput);
  27. Turn(turnInput);
  28. }
  29. private void Move(float input)
  30. {
  31. // Make sure to set drag high so the sliding effect is very minimal (5 drag is acceptable for now)
  32. // mention this trash function automatically converts to local space
  33. rb.AddForce(transform.forward * input * moveRate, ForceMode.Force);
  34. }
  35. private void Turn(float input)
  36. {
  37. transform.Rotate(0, input * rotationRate * Time.deltaTime, 0);
  38. }
  39. }