- Home /
Why does this cause velocity to rotate with the rigidbody2D?
I'm new to the whole game development thing here, and maybe I'm just being super dumb, but can someone please tell me why the following code allows the player to steer even when not applying force?
void FixedUpdate () {
rigidbody2D.AddRelativeForce (DriveSpeed * Vector2.up * Input.GetAxis ("Vertical"));
rigidbody2D.AddTorque (TurnSpeed * -Input.GetAxis ("Horizontal"));
var locVel = transform.InverseTransformDirection (rigidbody2D.velocity);
locVel.x = 0f;
rigidbody2D.velocity = transform.TransformDirection (locVel);
}
The intended outcome was to limit movement to only a single local axis (forwards, backwards), but instead I appear to have accidentally created some form of car. While I am interested in the best way to achieve this, I'm mainly trying to understand what I have actually done here.
Thanks for your help :)
Are you still adding toque and is your object against another object, like the floor?
Think of a car wheel. All it has is torque but it makes you move because of friction with the road...
Answer by TKS_Keeper · Jun 19, 2014 at 03:52 PM
To answer this we are going to break this down line by line:
/*
This line will apply a force along the Vector2.up axis in the vehicles space,
so if it has been turned it will go up relative to the vehicle and not
relative to the screen.
@see http://docs.unity3d.com/ScriptReference/Rigidbody2D.AddRelativeForce.html
*/
rigidbody2D.AddRelativeForce (DriveSpeed * Vector2.up * Input.GetAxis ("Vertical"));
/*
This line will add a rotational torque to the rigidbody, thus turning it
around its center of mass. If you wish to remove turning then remove this.
@see http://docs.unity3d.com/ScriptReference/Rigidbody2D.AddTorque.html
*/
rigidbody2D.AddTorque (TurnSpeed * -Input.GetAxis ("Horizontal"));
/*
This line retrieves the linear velocity in local space of the GameObject.
@see http://docs.unity3d.com/ScriptReference/Transform.InverseTransformDirection.html
*/
var locVel = transform.InverseTransformDirection (rigidbody2D.velocity);
/*
This line sets the x value to 0, so removing the x axis value of the
velocity.
@see http://docs.unity3d.com/ScriptReference/Transform.InverseTransformDirection.html
*/
locVel.x = 0f;
/*
This line transforms the new local velocity into world space velocity and
sets it directly. This is... not great, as it does cancle out any OTHER
external forces that may be acting on it and as the documentation states
this kind of change is normally done via a call to AddForce.
@see http://docs.unity3d.com/ScriptReference/Transform.InverseTransformDirection.html
*/
rigidbody2D.velocity = transform.TransformDirection (locVel);
Remember than when you apply Torque you turn the object, and applying a relative force will push it along the axis relative to the vehicle, not the game world. This means that as you turn the vehicle the direction of the force turns as well. IF what you were looking for was to move the object on the vertical axis of world (as if there were a rail traveling up and down that it was traveling along for example) and then turn in place along that rail (as if it were a turret which was spinning on the carriage that traveled on the rails in the previous example) then what you want to use is Rigidbody2D.AddForce instead of Rigidbody2D.AddRelativeForce. This will make it move along the WORLDS Vector2.up instead of the RigidBody's Vector2.up.
Now, if you want to actually implement vehicle like behaviors I suggest looking into Unitysteer which is an open source project by Arges Systems. They handle quite a lot of physics based steering implementation, and it's a fairly nice system which nicely abstracts away common behaviors into a set of configurable behaviors.
Your answer
Follow this Question
Related Questions
Physics.Raycast to Physics2D.Raycast? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Help with 2D physics script 1 Answer
Reduce Physics2D Lag 0 Answers