SHARE
TWEET

Untitled

a guest Jan 18th, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. [RequireComponent(typeof(Rigidbody))]
  6. public class PlayerMovement : MonoBehaviour
  7. {
  8.     [Header("Forward")]
  9.     public float maxSpeed = 12f;
  10.     public float acceleration = 24f;
  11.     [Header("Turning")]
  12.     public float maxTurnSpeed = 180f;
  13.     public float turnAcceleration = 720f;
  14.  
  15.     public string accelerateButton;
  16.     public string decelerateButton;
  17.     public string turnAxis;
  18.  
  19.     private Rigidbody rbody;
  20.     private Vector3 inputDirection = Vector3.zero;
  21.     private Vector3 wantedDirection = Vector3.zero;
  22.     private float forwardMove = 0;
  23.     private float turnMove = 0;
  24.     private float wantedTurn = 0;
  25.     private Vector3 wantedVelocity = Vector3.zero;
  26.     private bool shouldBreak;
  27.  
  28.     private void Awake()
  29.     {
  30.         rbody = GetComponent<Rigidbody>();
  31.     }
  32.  
  33.     // Update is called once per frame
  34.     void Update()
  35.     {
  36.         UpdateInputDirection();
  37.  
  38.         wantedDirection = transform.TransformDirection(new Vector3(0, 0, forwardMove));
  39.  
  40.         wantedVelocity = wantedDirection * maxSpeed;
  41.         wantedTurn = turnMove * maxTurnSpeed * Time.deltaTime;
  42.  
  43.         if (shouldBreak || forwardMove > 0)
  44.         {
  45.             rbody.velocity = Vector3.MoveTowards(rbody.velocity, wantedVelocity, acceleration * Time.deltaTime);
  46.         }
  47.         rbody.angularVelocity = new Vector3(0, wantedTurn, 0);
  48.     }
  49.  
  50.     void UpdateInputDirection()
  51.     {
  52.         if (Input.GetKeyDown(KeyCode.A))
  53.             rbody.AddForce(-transform.forward * 4, ForceMode.VelocityChange);
  54.  
  55.         forwardMove = (Input.GetKey(accelerateButton) ? 1 : 0);
  56.         turnMove = Input.GetAxis(turnAxis);
  57.  
  58.         //Dont use..
  59.         inputDirection.x = Input.GetAxis(turnAxis);
  60.         inputDirection.z = (Input.GetKey(accelerateButton) ? 1 : 0);
  61.  
  62.         shouldBreak = Input.GetKey(decelerateButton);
  63.     }
  64. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top