- Home /
Question by
Gohst002 · Jul 07, 2017 at 01:51 PM ·
car physics
Car controller help
Hi. I made this simple car controller and it does not quite work as well. When the car enters the air it bugs out a little and just drives forwards. Its quiet a long script and I will not be able to assist you that well because I am teaching myself to programm. using System.Collections; using System.Collections.Generic; using UnityEngine;
public class CarController : MonoBehaviour {
public float speed, acceleration, maxSpeed, returnSpeed, returnSpeedAcceleration, maxReturnSpeed, stopSpeed;
public float turnSpeed, maxTurnSpeed, turnAcceleration;
public Rigidbody rB;
public bool maxSpeedReached;
public bool driving, returning;
public bool maxTurnSpeedReached, turnLeft, turnRight, turning, canTurn;
public GameObject carController;
public bool isGrounded;
public bool canDrive;
public float currentSpeed;
public float gravity;
// Use this for initialization
void Start () {
rB = GetComponent<Rigidbody>();
maxSpeedReached = false;
canTurn = false;
}
// Update is called once per frame
void Update()
{
currentSpeed = speed;
Accelerate();
if (driving == true)
{
canTurn = true;
}
else
{
canTurn = false;
}
TurnCar();
CheckForGround();
}
public void CheckForGround()
{
RaycastHit hit;
if (Physics.Raycast(transform.position, Vector3.down, out hit, 1f))
{
isGrounded = true;
canDrive = true;
}
else
{
isGrounded = false;
canDrive = true;
}
Debug.DrawRay(transform.position, Vector3.down, Color.green, 1f);
if(isGrounded == false)
{
canTurn = false;
canDrive = false;
if(speed > 0f)
{
rB.useGravity = false;
speed = speed + acceleration + .2f;
transform.Translate(Vector3.down * gravity * Time.deltaTime);
}
}
if(isGrounded == true)
{
rB.useGravity = true;
canDrive = true;
canTurn = true;
}
}
public void Accelerate()
{
if (Input.GetKey(KeyCode.W) && canDrive == true)
{
driving = true;
speed = speed + acceleration;
}
else if(speed > 0f)
{
speed = speed - stopSpeed;
}
if(speed > 0f)
{
transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
else
{
driving = false;
}
if(speed <= 0f)
{
speed = 0f;
}
if(speed >= maxSpeed && driving != false)
{
speed = maxSpeed;
}
else
{
DeAccelerate();
}
if(Input.GetKey(KeyCode.S))
{
if (speed > 0f)
{
speed = speed - returnSpeedAcceleration;
}
else
{
canTurn = true;
returning = true;
returnSpeed = returnSpeed + returnSpeedAcceleration;
}
}
else
{
returning = false;
}
if (returnSpeed > 0f)
{
transform.Translate(Vector3.back * returnSpeed * Time.deltaTime);
}
if (returning == false)
{
canTurn = false;
returnSpeed = returnSpeed - .5f;
}
if(returnSpeed >= maxReturnSpeed && returning == true)
{
returnSpeed = maxReturnSpeed;
}
if (returnSpeed <= 0f)
{
returnSpeed = 0f;
}
}
public void DeAccelerate()
{
if (speed >= 0f && driving == false)
{
speed = speed - stopSpeed;
}
if(speed <= 0f)
{
speed = 0f;
}
}
public void TurnCar()
{
if(Input.GetKey(KeyCode.A) && canTurn == true)
{
turnLeft = true;
turning = true;
turnSpeed = turnSpeed + turnAcceleration;
transform.Rotate(0f,-turnSpeed * Time.deltaTime,0f);
}
else
{
turnSpeed = turnSpeed - 1f;
turnLeft = false;
turning = false;
}
if (Input.GetKey(KeyCode.D) && canTurn == true)
{
turnRight = true;
turning = true;
turnSpeed = turnSpeed + turnAcceleration;
transform.Rotate(0f, turnSpeed * Time.deltaTime, 0f);
}
else
{
turnSpeed = turnSpeed - 1f;
turnRight = false;
turning = false;
}
if(turnSpeed <= 0f && turning == false)
{
turnSpeed = 0f;
}
if(turnSpeed >= maxTurnSpeed)
{
maxTurnSpeedReached = true;
turnSpeed = maxTurnSpeed;
}
}
} I hope that someone will be able to help me. Thank you :D
Comment
Your answer
Follow this Question
Related Questions
Simple Kart Physics 0 Answers
Car driving in a spherical world. 2 Answers
2D Car rotation to forward and backward 0 Answers
Cant seem to make this basic 3D car turn properly 0 Answers
Question! Why does the 'WheelCollider' Component act funny? 1 Answer