- Home /
How do I make a vehicle turn properly?
When I tried using a movement script my car turns 90 degrees instantly instead of slowly turning like a real car would.
Here is the code Im using for it
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
[SerializeField] private Transform target; //created to attached object to
public float rotSpeed = 1.0f;
public float moveSpeed = 250.0f;
public float jumpSpeed = 275.0f;
public float gravity = -9.8f;
public float terminalVel = -300.0f;
public float minFallSpeed = -1.0f;
private float verticalSpeed;
private CharacterController _controller;
private ControllerColliderHit contactCollider; //used to store raycast information
private PlayerManager playerInfo; //creates a player profile to edit and add info
private Controller playerController; //recieve inputs from a controller
void Start()
{
verticalSpeed = minFallSpeed;
_controller = GetComponent<CharacterController> ();
playerInfo = GameObject.FindGameObjectWithTag("GameManager").GetComponent<PlayerManager> (); //find tag to access scripts from other gameobjects
}
// Update is called once per frame
void Update ()
{
//start @ 0,0,0 and add movement components progressively
Vector3 movement = Vector3.zero;
float horInput = Input.GetAxisRaw ("Horizontal");
float verInput = Input.GetAxisRaw ("Vertical");
if(horInput != 0 || verInput != 0)
{
//move while arrow keys are pressed
movement.x = horInput; //change movespeed to playerInfo.currentSpeed
movement.z = verInput; //how fast will the player move in that direction while holding the keys
movement = Vector3.ClampMagnitude (movement, playerInfo.topSpeed); //limit diagonal movement
//create new Quat to temporarily store the new angle
//print(transform.localEulerAngles.y); //angle that moves when player is turning
Quaternion temp = target.rotation;
target.eulerAngles = new Vector3 (0, target.eulerAngles.y, 0);
movement = target.TransformDirection (movement);
target.rotation = temp; //save to replace original rotation
//calculates a quat facing in that direction
//transform.rotation = Quaternion.LookRotation (movement);
Quaternion direction = Quaternion.LookRotation(movement);
transform.rotation = Quaternion.Lerp (transform.rotation, direction, rotSpeed * Time.deltaTime);
}
//if I hold down space
if(Input.GetKey(KeyCode.Space) || Input.GetKey(KeyCode.Joystick1Button0) )
{
brake ();
}
//if I let go of the brake button
if(!Input.GetKey(KeyCode.Space) || !Input.GetKey(KeyCode.Joystick1Button0))
{
//movement speed goes first to prevent speed from stopping at 1 due to update function
movementSpeed (); //increases speed when not braking
playerInfo.hasStopped = false;
}
movement *= Time.deltaTime;
_controller.Move (movement);
//move forward
movement.z = playerInfo.currentSpeed;
movement *= Time.deltaTime;
transform.Translate(movement);
}
void OnControllerCollidorHit(ControllerColliderHit hit)
{
contactCollider = hit;
}
void movementSpeed()
{
//player hasnt reached max speed
if(playerInfo.reachedMax != true && playerInfo.hasStopped != true)
{
playerInfo.currentSpeed += 1; //if not accelerate
}
//if at maxSpeed stop increasing
if(playerInfo.currentSpeed == playerInfo.topSpeed)
{
playerInfo.reachedMax = true;
}
//checks to change reachedMax incase of item pickup
else
{
playerInfo.reachedMax = false;
playerInfo.hasStopped = true;
}
}
//lower speed until 0
void brake()
{
//while slowing down to 0
if(playerInfo.currentSpeed >= 0)
{
//lower by 10 every frame?
playerInfo.currentSpeed -= 10;
playerInfo.hasStopped = false;
}
//keep current speed at 0 to prevent negative speed
if(playerInfo.currentSpeed <= 0)
{
playerInfo.currentSpeed = 0;
playerInfo.hasStopped = true;
}
}
//doesnt revert speed back to current
void boost() //boost boost boost boost
{
//create temp to revert boost back
float temp = playerInfo.currentSpeed;
if(Input.GetKeyDown(KeyCode.LeftShift))
{
playerInfo.currentSpeed *= 1.5f; //since its private it cant change values - going to public set should fix
}
if(Input.GetKeyUp(KeyCode.LeftShift))
{
playerInfo.currentSpeed = temp;
}
}
}
Comment
Answer by Sloth1998 · Apr 12, 2018 at 10:23 AM
You could try using Quaternion.Slerp function to smooth out the process of turning your car. https://docs.unity3d.com/ScriptReference/Quaternion.Slerp.html It basically requires two rotation values which are stored in the form of Quaternion and one float value, One is your current rotation state and the other is the rotation state you want to acquire. The float value is used to define the amount of smoothing required when transitioning from one state to another.