Facebook
From OWS, 4 Years ago, written in C#.
Embed
Download Paste or View Raw
Hits: 963
  1. using UnityEngine;
  2. using UnityEngine.InputSystem;
  3.  
  4. [RequireComponent(typeof(Rigidbody))]
  5. public class PlayerController : MonoBehaviour
  6. {
  7.     //Input fields
  8.     private FarmerInputActions farmerInputActions;
  9.     private InputAction movement;
  10.  
  11.     //physics settings
  12.     private Rigidbody rb;
  13.     [SerializeField]
  14.     private float movementForce = 1f;
  15.     [SerializeField]
  16.     private float jumpForce = 10;
  17.     [SerializeField]
  18.     private float maxSpeed = 5f;
  19.     private Vector3 forceDirection = Vector3.zero;
  20.  
  21.     [SerializeField]
  22.     private Camera playerCamera;
  23.  
  24.     private void Awake()
  25.     {
  26.         rb = this.GetComponent<Rigidbody>();
  27.         farmerInputActions = new FarmerInputActions();
  28.     }
  29.  
  30.     private void OnEnable()
  31.     {
  32.         //no event needed for movement
  33.         //these values will be constantly read
  34.         movement = farmerInputActions.Player.Move;
  35.         movement.Enable();
  36.  
  37.         //triggered by events
  38.         farmerInputActions.Player.Jump.performed += DoJump;
  39.         farmerInputActions.Player.Jump.Enable();
  40.     }
  41.  
  42.     private void OnDisable()
  43.     {
  44.         movement.Disable();
  45.         farmerInputActions.Player.Jump.Disable();
  46.     }
  47.     private void FixedUpdate()
  48.     {
  49.         //read input values
  50.         forceDirection += movement.ReadValue<Vector2>().x * GetCameraRight(playerCamera);
  51.         forceDirection += movement.ReadValue<Vector2>().y * GetCameraForward(playerCamera);
  52.         forceDirection *= movementForce;
  53.  
  54.         //apply the force
  55.         rb.AddForce(forceDirection, ForceMode.Impulse);
  56.         //reset for next frame
  57.         forceDirection = Vector3.zero;
  58.  
  59.         //extra gravity to feel less floaty?
  60.         if (rb.velocity.y < 0)
  61.             rb.velocity += Vector3.down * 10 * Time.fixedDeltaTime;
  62.  
  63.         //limit speed - but only in the horizontal
  64.         Vector3 horizontalVelocity = rb.velocity;
  65.         horizontalVelocity.y = 0;
  66.         if (horizontalVelocity.sqrMagnitude > maxSpeed * maxSpeed)
  67.             rb.velocity = horizontalVelocity.normalized * maxSpeed + Vector3.up * rb.velocity.y;
  68.  
  69.         MoveLookAt();
  70.     }
  71.  
  72.     private void DoJump(InputAction.CallbackContext obj)
  73.     {
  74.         if (IsGrounded())
  75.         {
  76.             forceDirection += Vector3.up * jumpForce;
  77.         }
  78.     }
  79.  
  80.     private void MoveLookAt()
  81.     {
  82.         Vector3 direction = rb.velocity;
  83.         direction.y = 0;
  84.  
  85.         if (movement.ReadValue<Vector2>().magnitude > 0.1f && direction.magnitude > 0.1f)
  86.             this.rb.rotation = Quaternion.LookRotation(direction, Vector3.up);
  87.         else
  88.             rb.angularVelocity = Vector3.zero;
  89.     }
  90.  
  91.     //Gets forward direction of camera relative to the horizontal
  92.     private Vector3 GetCameraForward(Camera camera)
  93.     {
  94.         Vector3 forward = camera.transform.forward;
  95.         forward.y = 0;
  96.         return forward.normalized;
  97.     }
  98.  
  99.     //Gets right direction of camera relative to the horizontal
  100.     private Vector3 GetCameraRight(Camera camera)
  101.     {
  102.         Vector3 right = camera.transform.right;
  103.         right.y = 0;
  104.         return right.normalized;
  105.     }
  106.  
  107.     private bool IsGrounded()
  108.     {
  109.         Ray ray = new Ray(this.transform.position + Vector3.up * 0.25f, Vector3.down);
  110.         if (Physics.Raycast(ray, out RaycastHit hit, 0.3f))
  111.             return true;
  112.         else
  113.             return false;
  114.     }
  115. }
  116.