Make the car stop by hitting the wall
Hi There! I'm trying to make a car game that when the car hits a wall it stops working.
The problem is that I only managed to make the upper parts of the car to stop, but the wheel colliders keep working and the car keeps going after hitting the wall. i've used the C# script:
public class CarScript : MonoBehaviour
{
public float maxTorque;
public Transform centerOfMass;
private bool isDead = false;
public WheelCollider[] wheelColliders = new WheelCollider[4];
public Transform[] tireMeshes = new Transform[4];
private Rigidbody m_rigidBody;
void Start()
{
m_rigidBody = GetComponent<Rigidbody>();
m_rigidBody.centerOfMass = centerOfMass.localPosition;
}
void Update()
{
if (isDead)
return;
UpdateMeshesPosition();
}
void FixedUpdate()
{
float steer = Input.GetAxis ("Horizontal");
float accelerate = Input.GetAxis ("Vertical");
float finalAngle = steer * 41f;
wheelColliders[1].steerAngle = finalAngle;
wheelColliders[0].steerAngle = finalAngle;
for (int i = 0; i < 4; i++)
{
wheelColliders[i].motorTorque = accelerate;
wheelColliders[i].motorTorque = maxTorque;
}
}
void OnTrigger (Collider other)
{
if (other.tag == "wall")
Death ();
}
private void Death()
{
isDead = true;
}
I'm new to Unity and as you can see, I do not have much KNOWLEGDE in programming and i think i did something wrong in this script, so... any help would be appreciated.
THANK YOU
*Sorry for my english, hope you can understand.
Also write this condition in FixedUpdate() function
if (isDead)
return;
it does'nt work, the car keeps moving. thank you for answer
Answer by RayCastX · Jun 09, 2016 at 03:27 AM
private void Death()
{
isDead = true;
this.GetComponent<Rigidbody>().isKinematic = true;
float steer = Input.GetAxis ("Horizontal");
float accelerate = Input.GetAxis ("Vertical");
float finalAngle = steer * 41f;
wheelColliders[1].steerAngle = finalAngle;
wheelColliders[0].steerAngle = finalAngle;
for (int i = 0; i < 4; i++)
{
wheelColliders[i].brakeTorque = Mathf.Infinity;
}
}
private void Respawn(){
isDead =false;
this.GetComponent<Rigidbody>().isKinematic = false;
}
Heey RayCastX Thanks for your answer! It worked for me. I had to make some changes 'cause the car wasnt moving, i don't know exactly why, so i did the following:
void OnTriggerEnter (Collider other)
{
if (other.tag == "Wall")
Death();
}
void FixedUpdate()
{
float steer = Input.GetAxis ("Horizontal");
float accelerate = Input.GetAxis ("Vertical");
float finalAngle = steer * 41f;
wheelColliders[1].steerAngle = finalAngle;
wheelColliders[0].steerAngle = finalAngle;
for (int i = 0; i < 4; i++)
{
wheelColliders[i].motorTorque = accelerate;
wheelColliders[i].motorTorque = maxTorque;
{
if (isDead)
wheelColliders[i].brakeTorque = $$anonymous$$athf.Infinity;
}
}
}
private void Death()
{
isDead = true;
this.GetComponent<Rigidbody> ().is$$anonymous$$inematic = true;
}
private void Respawn()
{
isDead = false;
this.GetComponent<Rigidbody> ().is$$anonymous$$inematic = false;
}
Thanks a lot for the help!
Your answer
Follow this Question
Related Questions
How to make brake car ? 2 Answers
How to teleport a car? 1 Answer
wheel collider / script offsets the meshes 0 Answers
Unity2d move enemy in opposite direction on collision 1 Answer
The laser problems 0 Answers