- Home /
How can I make my truck drive on terrain and on bridge
I have my game set up to where if the player is driving and loses contact with the terrain, he loses control of the vehicle. Basically the logic is that all user input code is wrapped in an if statement checking if groundContact is true. OnCollisionEnter, if the object you're colliding with equals "Terrain," groundContact is true. OnCollisionExit, if the object you WERE colliding with equals "Terrain," groundContact is false. I tried making it like this instead:
function OnCollisionEnter(grounder : Collision){
if(grounder.gameObject.name == "Terrain" || grounder.gameObject.name == "Bridge"){
groundContact = true;
}
}
function OnCollisionExit(grounder : Collision){
if(grounder.gameObject.name == "Terrain" || grounder.gameObject.name == "Bridge"){
groundContact = false;
}
}
I even tried checking both of them at the same time by a user defined tag and set both the bridges and the terrain to said tag. I know it's that OnCollisionExit thats breaking it. The player transitions onto the bridge from the Terrain without ever leaving the ground, but since it exits the collision with Terrain, it sets groundContact to false while you're also colliding with Bridge. Any ideas?
Try it using OnCollisionStay ins$$anonymous$$d of OnEnter, and maybe it'll fire right so if you lose contact on exit/re-enter it won't matter.. and/or you could also do some raycasting to check your grounding... and/or you could use wheel colliders and http://docs.unity3d.com/ScriptReference/WheelCollider.GetGroundHit.html to check and see if all of your wheels aren't hitting the right ground then you're not grounded.
Edit : just in case you try the wheel thing, I'd only check one at a time so you don't waste time with others if it's not necessary. Check wheel1, if it's good, then screw the rest cuz we got one touching and thats good enough; if its not touching, check wheel2 and do the same... and so-on.
Answer by DarkGamer67 · Jul 19, 2014 at 07:55 PM
I wouldn't use the name. It's better to use Tags. That's how you define Tags : http://docs.unity3d.com/Manual/Tags.html
Your script would look like this (if you tag your Terrain and Bridge ground):
function OnCollisionEnter(grounder : Collision){
if(grounder.gameObject.tag == "ground"){
groundContact = true;
}
}
function OnCollisionExit(grounder : Collision){
if(grounder.gameObject.tag == "ground"){
groundContact = false;
}
}
Your answer
Follow this Question
Related Questions
OnCollisionExt not working/ Alternate collision detection 1 Answer
OnCollisionEnter not working. 2 Answers
Collision with Terrain 2 Answers