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.