- Home /
Player vehicle Movement using rigidbody
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.
1) What should I be using to move rigid body forward and backwards? forces or velocity?
2) What should I be using to turn rigid body while moving forward? forces, torque, or angular velocity?
3) 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);
}
}
If you want to emulate a car or a vehicle, I suggest you use WheelCollider
s or your own implementation thereof. You aren't going to get anything that looks good using just standard rigidbody dynamics :)
well my game is futuristic there are no wheels. I don't care how it looks that's not my portion I just need it to act like a car does. Are you saying that the only way to reach my goal is to add wheel colliders?
If you want realistic vehicle physics that resemble that which we have today, then yes, you should use WheelCollider
s
If you want realistic physics, then I suggest finding out how your "car without wheels" works and then implement the physics using that.
It really depends on what you want the final outcome to be, and since you have not given such information, all I can do is guess
Answer by scipiothegreat · Aug 24, 2013 at 02:24 AM
The best way to add force gradually is to use rigidbody.AddForce(). Changing the velocity results in the car going from 0-60 in one frame. For turning use Torque, not angularVelocity. A top speed can made by checking the rigidbody's velocity.magnitude or velocity.sqrMagnitude. Braking and drag can be applied by applying a force in the opposite direction of movement.
The problem kinda isnt how to make the car move, is that, I use the rigidbody.AddFoce() to make it go right or left, but then it either moves perfectly but doesnt rotate at all, which makes the car look like its sliding rather than turning, and by using my solution it turns and even wobbles a bit (drift/drag I assume its called) before stabilizing but instead of stabilizing in the same direction it was going before it turned it now takes another diagonal direction...