Hi guys,
Need some real help here for a school project and I am a novice with unity. I am aiming for player movement using rigid body that works similar to a street vehicle. I need physics that accelerates through to a top speed and once in motion I want the object to stay in motion until drag slowly stops it or brakes are applied. The code I have at the bottom moves like a boat in water, it doesn’t have good turning and some weird rotation issues. plus no gauge for top speed. So I mainly narrowed it down to three questions for you guys.
-
What should I be using to move rigid body forward and backwards? forces or velocity?
-
What should I be using to turn rigid body while moving forward? forces, torque, or angular velocity?
-
How should I accelerate gradually to a top speed or velocity?
here is my code that I have been doing for about two weeks and tried every kind of combination that I know. So any help with this would really be appreciated.
var tForce : float = 100;
var mForce : float = 300;
var drag : float = 1.5;
var forward : boolean = false;
var backward : boolean = false;
var right : boolean = false;
var left : boolean = false;
var r_angleVelocity : Vector3 = Vector3 (0,100,0);
var l_angleVelocity : Vector3 = Vector3 (0,-100,0);
var forwardVector : Vector3;
var rightVector : Vector3;
var leftVector : Vector3;
var forwardLeft: Vector3;
var forwardRight: Vector3;
function Update ()
{
if(Input.GetKey("w"))
{
forward = true;
}
else
{
forward = false;
}
if(Input.GetKey("s"))
{
backward = true;
}
else
{
backward = false;
}
if(Input.GetKey("d"))
{
right = true;
}
else
{
right = false;
}
if(Input.GetKey("a"))
{
left = true;
}
else
{
left = false;
}
}
function FixedUpdate()
{
var r_deltaRotation : Quaternion = Quaternion.Euler(r_angleVelocity * Time.deltaTime);
var l_deltaRotation : Quaternion = Quaternion.Euler(l_angleVelocity * Time.deltaTime);
forwardVector = (Vector3.forward * mForce);
rightVector = (Vector3.right * tForce).normalized;
leftVector = (Vector3.left * tForce).normalized;
forwardLeft = (forwardVector + leftVector).normalized;
forwardRight = (forwardVector + rightVector).normalized;
if(forward == true)
{
rigidbody.AddRelativeForce(forwardVector,ForceMode.Force);
}
else
{
rigidbody.drag = drag;
}
if( backward == true)
{
rigidbody.AddRelativeForce(Vector3.back * mForce, ForceMode.Force);
}
if( right == true)
{
rigidbody.MoveRotation(rigidbody.rotation * r_deltaRotation);
rigidbody.AddForce(forwardRight, ForceMode.Acceleration);
}
if( left == true)
{
rigidbody.MoveRotation(rigidbody.rotation * l_deltaRotation);
rigidbody.AddForce(forwardLeft, ForceMode.Acceleration);
}
}