x


Child Colliders, Triggers and One script to rule them all

I am creating a flight sim. And now I am in the middle of collision detection script.

Here is my script:

var groundExp:GameObject;
var jetModel:GameObject;
var jet:GameObject;
var terrain:GameObject;

function OnTriggerExit (other : Collider)
{
   if(other.gameObject.tag == "Border")
 {
 Debug.Log("Out of Bounds!");
 }
}

function OnTriggerEnter (other : Collider) {

 if(other.gameObject.tag == "Ground")
 {
 print("Crashed");
 JetPhysicsMovement.pov = 2; //Change the POV to 3rd person.
 jetModel.gameObject.SetActiveRecursively(false); //Disable all jet models and etc.
 jet.rigidbody.velocity = Vector3.zero; //I don't want it to keep moving.
 var expGround = Instantiate(groundExp, jet.transform.position,
Quaternion.identity); //Nice explosion from detonator.
     coroutine();
 }

   if(other.gameObject.tag == "Water")
 {
 Debug.Log("Crashed into water!");
 JetPhysicsMovement.pov = 2; //3rd persom view
 terrain.collider.isTrigger = false; //this will be explained in problem 3.
 collider.isTrigger = false; //this will be explained in problem 3.
 jet.rigidbody.velocity = Vector3.forward*20; //for a sink effect.
 coroutine();
 }
}

function coroutine()
{ 
 yield WaitForSeconds(2); //Let the player see the crash for 2 seconds.
 jetModel.gameObject.SetActiveRecursively(true); //bring back all the objects.
 Respawn();
}

function Respawn()
{
terrain.collider.isTrigger=true;
collider.isTrigger = true;
jet.transform.position = Vector3(30,150,30);
jet.transform.rotation = Quaternion.identity;
jet.rigidbody.AddForce(Vector3.forward*75,ForceMode.Impulse);
}

I have an EmptyObject which I named Jet. which is the father of all children objects (paritcle effects, model, colliders, spawnpoints and etc.) for the collider I have another father which has only Rigidbody and isKinematic is positive. It has 5 children colliders which are all triggered.

a quick explanation for the variables: jetModel -> is only the jet model, which also hold the particle effect (engine).

jet -> is the main EmptyObject which holds everything.

terrain -> is the terrain.

groundExp -> is the crash explosion.

My script was working, when I had mesh, and convex, however, mesh is glitchy anyway in Unity3d so I had to add multiple specific colliders.

Right now, when everything is triggered on, the only thing that works is the OnTriggerExit, when I'm out of bounds.

I have several issues.

When none of my triggers are triggered, Unity from some reason actually detect the collision, and the jet explodes when hits the ground.

Problem 1: sometimes the particle effects remain for a moment until the script disable all of the children. (which I don't know why it has a delay)

Problem 2: when I hit the water, which actually several colliders hit the water, then my script runs multiple times for every collider that has been collided.

Problem 3 (which related to 2): When I hit the water, I added a little effect which let the jet "sink", However when it collides with the ground (terrain) it explodes in the water.

I had a fix for that, which I disabled the triggers for the collision and then even if the jet hits the terrain in water, it doesn't explode. However, I can't access it from 2 reasons:

A) There are no triggers actually working, if they did, unity wouldn't detect the collision. B) I don't know how to access all of the triggers and disable them.

more ▼

asked Jul 10 '12 at 01:56 PM

Ranger Ori gravatar image

Ranger Ori
108 3 10 15

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Okay I have fixes for my own problems.

1) It was a collision with 2 scripts, which 1 turned the particles on, and another script that tried to shut it off.

2) What I did was shutting off "isKinematic" as soon there's a collision. very rarely I have to exact collision. so I did lower a bit one of the colliders to make sure it won't happen.

3) This is related to my 2nd solution.

more ▼

answered Jul 23 '12 at 06:15 AM

Ranger Ori gravatar image

Ranger Ori
108 3 10 15

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3316
x1780
x1682
x980
x183

asked: Jul 10 '12 at 01:56 PM

Seen: 763 times

Last Updated: Jul 23 '12 at 06:15 AM