LAKster
LAKster

Reputation: 149

Rigidbody not acting as expected

For the past few days I have been working on some custom car physics as a coding exercise.

What my ultimate goal is to have some semi-realistic car physics something in between the quality of the older GTA games and the recent GTA 5.

Now what I got so far is a quite intricate process which eventually gives me a float which is the exact speed I want the car to go until the next frame, so far I have used the following code to do that;

this.transform.Translate (0, 0, speed * Time.deltaTime);

However because I want to use the Unity build in physics for collision detection I realised this wouldn't work because transform.Translate (as far as I know) literally places the object at that position, thus the object could (if going fast enough) suddenly be halfway stuck through a wall or just completely ignore the collision and appear on the other side of the wall. Now instead I decided to do this:

rb.velocity = transform.forward * speed;

To quickly note, I am doing both of these in my FixedUpdate and I am currently only using the second example (or at least trying to use it and am miserably failing at it). Due to it being in the fixed update I should be able to at least test it to a certain extend without Time.deltaTime usage, which is why you aren't seeing that in the second example right now.

To continue my story, for some reason when I set the velocity of the rigidbody instead of using transform.Translate my collision acts really strangely. The car becomes all floaty and nudges forward (stands on its nose) when I hit the wall, I have no clue what could be causing this seeing as I am using my own gravity (the gravity from the original Rigidbody is turned off in favor of my own custom gravity, which does result in a downward rigidbody force) and the drag, and angular drag of the rigidbody have been turned down to 0.

Basically my question is do any of you guys have an idea of what could be causing my rigidbody to be reacting in such a manner, literally the only difference between my normal code and the code that is acting up is that single line change.

I've looked in to using rigidbody.AddForce() instead but that just doesn't seem to do anything at all and on top of that the whole reason why I am writing my car script is to determine the velocity the object should be moving at thus making something like rigidbody.AddForce() which adds it to the velocity of the car more or less redundant and shouldn't be necesarry.

The only reason why I'm even using a rigidbody in the first place is so I can do two things: 1. Easy collisions. It means I can add simple forces such as gravity to it and not have to worry about it going through the floor. 2. easy rotations when affected by physics such as collisions.

If anyone has any ideas on how to do these two things in a custom script I'd much rather do that because as much as I like unity the Unity rigidbody physics are practically worthless in my eyes, however a must have because I can't write my own custom rigidbody and I can't find the source code of the Unity Rigidbody either. What I'd rather have is a stripped down version of the rigidbody which allows for easy collisions and easy rotation but without all the stuff like the build in drag or velocity because these are the exact things I want to be able to control myself and have been able to control myself so far when I use transform.Translate, but I lose this control when I have to use rigidbody.velocity.

Upvotes: 2

Views: 1698

Answers (3)

Ruzihm
Ruzihm

Reputation: 20269

Replace the Translate with the following:

rb.MovePosition(transform.position + transform.forward * (speed * Time.deltaTime));

MovePosition basically acts as a translate (if you do it like this) except it also counts collisions in to it.

Upvotes: 0

mgear
mgear

Reputation: 1908

If you want super realistic 3D car physics, should look into ready assets like: Edy's Vehice Physics https://www.assetstore.unity3d.com/en/#!/content/403

Also from Unite2015 talks, see example project: "EasySuspension" to build sample 3D car by script: http://bit.ly/1y8ucNW (from video: https://youtu.be/WGvuP6vV6j4?t=17m8s )

Or is the game top down 2D game? (like early GTA's)

Upvotes: -1

Al Wyvern
Al Wyvern

Reputation: 199

according to the documentation you shouldn't use rb.velocity to change the velocity regularly - it could result in unrealistic physics

http://docs.unity3d.com/ScriptReference/Rigidbody-velocity.html

instead use rb.addforce

http://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html

hopefully that will solve the issue!

Upvotes: 2

Related Questions