Wednesday, May 29, 2019

unity - Car driving through walls with transform.Translate()


I need help with a script for a very simple car that uses transform.Translate:


public class car1 : MonoBehaviour {

public PauseMenu pause;
[Space]
public Car_Script car_s;
public float speed;

public GameObject car;

void FixedUpdate()
{

if (car_s.InCar == true)
{
if(pause.isPaused == false)
{
if (Input.GetKey(KeyCode.W))

{
car.transform.Translate(Vector3.forward * Time.deltaTime * speed);

if (Input.GetKey(KeyCode.A))
{
if (speed <= 0)
{
}
else
{

if (speed > 0F && speed <= 50F)
{
car.transform.Rotate(Vector3.down, 2F);
}
else if (speed > 50F && speed < 90F)
{
car.transform.Rotate(Vector3.down, 1.3F);
}
else if (speed > 90F)
{

car.transform.Rotate(Vector3.down, 0.9F);
}

}
}
if (Input.GetKey(KeyCode.D))
{
if (speed <= 0)
{
}

else
{
if (speed > 0F && speed <= 50F)
{
car.transform.Rotate(Vector3.up, 2F);
}
else if (speed > 50F && speed < 90F)
{
car.transform.Rotate(Vector3.up, 1.3F);
}

else if (speed > 90F)
{
car.transform.Rotate(Vector3.up, 0.9F);
}

}
}
}
}
}

}
}

Whenever I drive the car it immediately goes through walls.


I have colliders on everything. The car has mesh colliders set to convex.


I've searched around quite a bit for answers and it seems that a common one is using rigid bodies. But whenever I even put a rigid body on the car, it flies out into the sky.




No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...