Hey all. I’m making a lil racing game to distract myself from school, and it’s a little crazy. I made a 3D Go-Kart and applied a wheel collider to all of the wheels. But when I try to AddForce forward the whole thing starts spinning around its x axis and falls through the floor! Can anybody gimmie some tips? Thanks.
You don’t use AddForce with wheel colliders. With them you apply motorTorque, brakeTorque and set steerAngle on the wheels from a script (see the documentation and the race demo example project).
AddForce method would be used if you’d use raycast colliders to approximate the wheels.
Ohhhhh. Thanks-a pal. I’ll let you all see my racing game when I have something fun/playable. n_n
It gives me an error saying that motorTorque isn’t a real variable.
From the docs it looks like it is a variable. Can you post a little code snippet?
function Update () {
if (motorised == true) {
if (Input.GetKey("up")) {
collider.motorTorque = 20;
}
}
}
where is motorised declared?
Otherwise the script compiles fine for me. What compile error exactly are you getting.
motorized is declared earlier in the script. It isn’t a compile error, it’s just when I press the Up key it yells at me that motorTorque isn’t declared. I thought it was supposed to be inherited by Wheel Colliders.
Is that script attached to the wheel Game Object, or is it attached to something else, like the parent object that may be the car’s body? If it is attached to something like the car’s body then the collider it is getting in your statement is not a wheel collider, unless of course you have a wheel collider attached to the car’s body . Basically, do you have 4 of your script with one attached to each wheel?
Yep. 4 wheels with wheel colliders all with this script on 'em.
If you click on the error in the console, it draws a line to the object which caused the error, that will probably tell you more which wheel failed and then you can see if there is something wrong in the setup there.
Alternatively replace this
collider.motorTorque = 20;
with this:
GetComponent(WheelCollider).motorTorque = 20;
In case there is no WheelCollider attached, the error in the console will also tell you name of the object etc.
var motorTorque is declared in the car.js script i believe. that should be on your parent object. your wheel colliders should have the wheel.js script attached. is that how you have it?
Joe: I’ll try what you said when I get home (I’m in MSOffice Class).
Pete: I’m not using the driving example. I tried and it just gave me grief. I have MY Wheel.js attached to all four wheels.
but motorTorque is declared in car.js so without it, motorTorque isn’t a valid variable because it hasn’t been declared if you don’t have the car script attached to your parent.
motortorque is declared in the wheel collider.
are you sure? the one i’m looking at has it declared in car.js and i’m pretty sure i didn’t change that. motor is also declared there but moldy isn’t using that.
the only reference to motorTorque in wheel.js that i could find is
if( motorised )
collider.motorTorque = motor;
are you thinking of motorised? that’s declared in wheel.js
Look at the documentation for Wheel Colliders or for motorTorque. It doesn’t say anything about the car.js script.
ah. poof lightbulb went off. duh. misread joe’s post and yours! maybe i ought to rtm someday! anyone reading this thread ignore my above posts or you’ll be as confused as i was… sorry!
I’m having a difficult time putting a car together without the old racing tutorial to guide me. Does anybody have that around?
Mainly, I’m trying to figure out how to properly orient a “car” with steerAngle and get it moving with motorTorque. As it is, the wheel colliders are rotating and receiving a torque value, but nothing useful is going on because of that.
Edit: here we go…hopefully this will get me on the right path. Right now, I’m not sure that I have the correct parent/child relationship going on, because my car won’t accelerate or turn.
http://forum.unity3d.com/viewtopic.php?t=11101&highlight=racing