I'm having a problem crashing my jet into the ground for the flying game. It keeps just passing though, and I want it to vanish. Can anyone help? thanks.
here is some code:
function OnControllerColliderHit(theCollision : Collision){
if (theCollision.gameObject.name == "Ground"){
Destroy(gameObject);
}
}
EDIT: I have colliders on both my jet and the terrain.
4 Answers
4
For starters, you have to be moving your object with Controller.Move() to have the collisions calculated for the CharacterController and to have the OnControllerColliderHit() to be called. If you are not doing that, then you won't get collisions/messages.
Another note, you probably don't want to have a character controller on a jet. CharacterControllers are designed for characters. They don't work well for other things. The shape hints at a bipod, and you have to add your own physics.
I would suggest that you use Rigidbodies and a combination of colliders (or a mesh collider) to control your jet.
If you are using a rigidbody for your player then it needs colliders (try using primitives instead of a mesh collider) to detect collisions and like Peter G says it needs to use rigidbody.AddForce (or .velocity) If you are using a character controller make sure you are using transform.translate to move the CHARACTER CONTROLLER and not transform.position to move the OBJECT (which is where I made my 1st ever unity mistake the moment i tried scripting)
k, I made it into a rigid body and revised the code to this:
function OnCollisionEnter(theCollision : Collision){
if (theCollision.gameObject.name == "Ground"){
Destroy(gameObject);
}
}
however, it still doesn't seem to work as the jet just phases though the ground still. Am I doing this right? thanks for the replies.
K, im not sure if I worded it right. What I want is for the plane to just dissapear (or stop dead) when it hits the terrain. I don't want it to bounce or anything like that. Its a case where I want the plane to perform a function when a collision is triggered. I tried some of the above ideas, but I still haven't been able to get the plane to do this.
Sorry.
But, yeah, while these ideas are great, the problem is still unsolved for me. Any more help is much appreciated, and thankyou to those who took time out their day to help me :)
if your terrain and jet are too big the collision detection will be too much for unity to handle. What kind of collision are you using for the jet? Mesh, box, sphere, etc
– anon61858128Im using a mesh collider
– anon21826004