Moving a car from the top

Hi, sorry, it may be a very noob and stupid question, I want to make a car to run and turn from the top view (micromachine style), but I can’t combine both position and rotation when I want the car to turn. Can someone tell me the easiest way to do that please?.

Care to show what you tried?

Use simply Rigidbody (look for examples) on the car object, provided is a simple mesh without unity wheels system.
Then you need to look at add relative force and add torque to keep it simple.
Something to start of.

1 Like

Thanks a lot, I finally did with Translate and Rotate in transform. But doesn’t work for Rigidbody, I can only move forward/backwards by Addforce, but when I use rotation simply rotates in the same axis, don’t know how to combine it with rotation, any clue?

void Update () {
float turnfactor = Input.GetAxis(“Horizontal”) * turn;
float direction = Input.GetAxis(“Vertical”) * speed;
transform.Translate(0, direction, 0); //rb2d.Addforce(new Vector3(0,direction,0);
transform.Rotate(0, 0, -turnfactor); //rb2d.rotation??
}

for spinning try

Rigidbody2D rb2d = myCarObject.GetComponent <Rigidbody2D> () ; // call GetComponent it in start, at initialization, not in man update (I think you got this correct)

// rest in update

rb2d.AddRelativeTorque ( Vector3.up * torque ) ;
// where torque will be your turn turnfactor
// like this
rb2d.AddRelativeTorque ( Vector3.up * turnfactor ) ; // Vector3.up = new Vector ( 0, 1, 0 ) ;

Mass of the car will affect the behavior, but you add dynamics to the game
If you use position using transform, you run to problems later, i.e. collisions.

To add forward force, as I said, add relative force not just force.
Id it possible just force, but will require extra little car rotation (see below velocity) ;

rb2d.AddRelativeForce ( Vector3.forward * throttle) ; // Vector3.forward= new Vector ( 0, 0, 1 ) ;
// where throttle can be your direction float

Alternativelly you can set velocity directly
You will need get forward vector, by using rotation of the car
Brackets are just for clarification and order importance.

rb2d.velocity = ( rbrb2d.rotation * Vector3.forward ) * speed ;
// where again, speed is your direction float
1 Like

Thanks a lot, I don’t have AddRelativeTorque, just AddTorque(float) where I pass turnfactor as float. The result is ok but the car is very, very difficult to manage.

Ah right, that is because of Rigid Body for 2D not 3D, which I work normally.
Sorry for confusion. But this way in 2D is just simpler indeed.

But perhaps whatever problem you are facing, you would need to overcome ^^.
You can always try to combine add relative force, and set rotation.
But again, I would expect some unwanted collision behaviors, during rotations.

I suggest, to check other 2D racing games for ideas, how they have been made.

1 Like