- Home /
Modify Rigidbody Speed
How would I modify my rigidbody's speed? I have a tank using WheelColliders that can chug along towards you. The problem is, it can't climb hills. I have a script to fix that. It speeds up the tank if it slows down while trying to climb a hill, and catches it if its going too fast.
var speed = Rigidbody.velocity; var maxSpeed; var minSpeed;
function Update () { if (speed < minSpeed) { motorTorque ++; }
if (speed > maxSpeed) { motorTorque --; } }
However, this script needs fixing. What's wrong?
P.S. It gives this error message: "An instance of type 'UnityEngine.Rigidbody' is required to access non static member 'velocity'."
I tried to modify it a bit, and then it got all messed up. So I took it down for a sec, fixed it, and re-posted it. It's back up now.
Answer by Eric5h5 · Mar 16, 2010 at 08:47 PM
Several things are wrong...mainly, Rigidbody.velocity is a Vector3, which you can't change with ++ or --; you have to say what member(s) of Vector3 exactly you want to change. You may have been thinking of Rigidbody.velocity.magnitude, but that's read-only. So you need to change the entire Vector3.
Two, you've assigned Rigidbody.velocity to a variable called speed, which even if you were modifying correctly, you're doing nothing with it. Changing the speed variable won't have any effect on the velocity.
Three, even if that code did work, it's framerate-dependent, where what happens will happen at different rates as the framerate goes up and down, and on different machines.
Four, while it's not exactly wrong, you have minSpeed and maxSpeed as dynamic variables for no good reason, which will work, but slowly. You should specify the type or let the compiler infer it. (i.e., var minSpeed = 10.0;)
I guess he is more interesting for an answer to the first question.. "How would I modify my rigidbody's speed?"
Yeah. It gets stuck at little tiny bumps and if I could modify the speed, or wheel speed maybe, I could get it to speed over the bump and then slow back down. I guess I was going about it all wrong.
Er, well, now the code that I replied to is different.... It's still framerate-dependent, but what is motorTorque referring to? Do you mean collider.motorTorque? Also, you're only reading Rigidbody.velocity into speed once; you need to do it every frame (and it should be Rigidbody.velocity.magnitude). Although FixedUpdate would be better here since you're using physics.
Oops. I changed Rigidbody.velocity to motorTorque because I heard that you shouldn't touch R.velocity. I'll change it to FixedUpdate... As you can see, I'm fairly new to scripting, I just assumed that that would work. I seem to be out of my depth here - would you $$anonymous$$d writing a script or at least a framework I can modify? Any help greatly appreciated...
Answer by devilkkw · Mar 16, 2010 at 08:36 PM
var speed = Rigidbody.velocity.magnitude; var maxSpeed:float=40; var minSpeed:float=10;
function Update () { if (speed < minSpeed) { speed ++; }
if (speed > maxSpeed) { speed --; } }
That won't have any effect; all you've done is assign a type to the maxSpeed and $$anonymous$$Speed variables, which, while it is an improvement, isn't the problem.