Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- [RequireComponent(typeof(Rigidbody))]
- public class PlayerMovement : MonoBehaviour
- {
- [Header("Forward")]
- public float maxSpeed = 12f;
- public float acceleration = 24f;
- [Header("Turning")]
- public float maxTurnSpeed = 180f;
- public float turnAcceleration = 720f;
- public string accelerateButton;
- public string decelerateButton;
- public string turnAxis;
- private Rigidbody rbody;
- private Vector3 inputDirection = Vector3.zero;
- private Vector3 wantedDirection = Vector3.zero;
- private float forwardMove = 0;
- private float turnMove = 0;
- private float wantedTurn = 0;
- private Vector3 wantedVelocity = Vector3.zero;
- private bool shouldBreak;
- private void Awake()
- {
- rbody = GetComponent<Rigidbody>();
- }
- // Update is called once per frame
- void Update()
- {
- UpdateInputDirection();
- wantedDirection = transform.TransformDirection(new Vector3(0, 0, forwardMove));
- wantedVelocity = wantedDirection * maxSpeed;
- wantedTurn = turnMove * maxTurnSpeed * Time.deltaTime;
- if (shouldBreak || forwardMove > 0)
- {
- rbody.velocity = Vector3.MoveTowards(rbody.velocity, wantedVelocity, acceleration * Time.deltaTime);
- }
- rbody.angularVelocity = new Vector3(0, wantedTurn, 0);
- }
- void UpdateInputDirection()
- {
- if (Input.GetKeyDown(KeyCode.A))
- rbody.AddForce(-transform.forward * 4, ForceMode.VelocityChange);
- forwardMove = (Input.GetKey(accelerateButton) ? 1 : 0);
- turnMove = Input.GetAxis(turnAxis);
- //Dont use..
- inputDirection.x = Input.GetAxis(turnAxis);
- inputDirection.z = (Input.GetKey(accelerateButton) ? 1 : 0);
- shouldBreak = Input.GetKey(decelerateButton);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement