- Home /
Why is my object tilting when moving?
Hello. I am using addRelativeForce to control a rectangle collider. However, it tilts whenever it is supposed to only move forward. How do I prevent this?
var forwardMoveAmount = -30; //Sets forward speed.
var turnForce = 2; //Sets turning speed.
var canDrive = 0;//Sets up ground detection (For now, works with all objects)
function Start ()
{
rigidbody.centerOfMass = Vector3 (0, 0, 0);//Keeps object upright.
}
function OnCollisionEnter (col : Collision)
{
canDrive = 1;//Gives player control entering on ground.
if (col.gameObject.name == "GoingUp")//Check if on a jump pad.
{
rigidbody.velocity = (Vector3.up * 30);//Makes player fly upwards.
}
}
function OnCollisionStay (col : Collision)//Gives player control on ground.
{
canDrive = 1;
}
function OnCollisionExit (col : Collision)//Removes player control off ground
{
canDrive = 0;
}
function Update () {
var forwardMoveAmount = Input.GetAxis("Vertical") * forwardMoveAmount * canDrive;//Get input, multiplies it by movement amount and if the player can drive.
var turnForce = Input.GetAxis("Horizontal") * turnForce;//Get input and multiplies it by turnFroce
transform.Rotate (0,turnForce * canDrive,0);//Rotates if canDrive is not 0
rigidbody.AddRelativeForce (Vector3.right * forwardMoveAmount);//Applies relative force to object to move it
}
Here is my whole player code in case it helps. Apologies if its a bit unprofessional, I'm quite new to unity.
Answer by SparkZWolf · Jun 16, 2014 at 01:09 PM
The friction of the ground was rubbing on the collider. Sorry for necroposting, but someone helped me over on reddit. Thanks though!
Answer by nesis · Jan 28, 2014 at 10:03 PM
You can prevent the rigidbody from rotating by setting its Freeze Rotation parameter in the Inspector tab.
Thank you, but the object need to be able to rotate when I want it to. A freeze would prevent it from turning entirely. I just want to apply a force so that it moves only in one direction without turning.
You can store your rigidbody's angularVelocity in a Vector3, add the force, then set angularVelocity to the value you stored, if that approach works as you want.
I still suspect that locking the rotation of your rigidbody on one axis is what you're wanting...?
Your answer
Follow this Question
Related Questions
Spaceship physics - adding resistance 2 Answers
Set velocity of kinematic rigidbody appropriately 1 Answer
how to create the magnet similar to the real magnet ? 1 Answer
Problem with Rigidbody Parent/Child Relationship with "Teeter Totter" 0 Answers
When I try to save the velocity of a rigidbody and use it later it doesn't work? 1 Answer