I have different car animation running on the road which have imported from max. I want to make such an intelligent System that if car start to trigger from another car it become stop. So far I have tried this.

  1. Added four collider (as you can see in Fig 1) with tag name car, carBack, carLeft, CarRight enter image description here

  2. car left/right contains this code. Added this script to car left,right collider that if this collider hit with car object then car become stop.(And on trigger exit it resume)

     void OnTriggerEnter(Collider c)
            if (c.tag == "Car")
                Debug.Log("collided with : carsideTriggerScript :" + c.transform + " - object Name to stop : " + gameObject.name);
                c.transform.gameObject.GetComponent<AnimationControlSpeed>().Speed = 0;
        void OnTriggerExit(Collider c)
            if (c.tag == "Car")
                Debug.Log("Exi collided with :  carsideTriggerScript:" + c.transform + " - object Name to resume : " + gameObject.name);
                 c.transform.gameObject.GetComponent<AnimationControlSpeed>().Speed = 1;
  3. Added script which check that if car start to collide with carBack collider then stop (And on trigger exit it resume. Same script as above).

    But a scenario is creating which you can see in fig 2.

enter image description here

I am enable to handle it. it seems that one car collider hitting carRightCollider and same thing happen with another car so that both car become stop and unable to move again. How can i control it? as same scripts are attached to every gameobject with same OntriggerEnter and logic.

  • \$\begingroup\$ Are all the colliders on seperate GameObjects? \$\endgroup\$ – hobnob Jun 23 '16 at 10:55
  • \$\begingroup\$ each car have a own colliders \$\endgroup\$ – Muhammad Faizan Khan Jun 23 '16 at 11:05
  • 1
    \$\begingroup\$ But for each car are there child game objects that hold the separate colliders. For example, does Car1 have 4 child game objects (Left, Right, Top, Bottom) colliders? If not, it might be worth separating them out into game objects. That way you can add your script to just the left/right collider child objects \$\endgroup\$ – hobnob Jun 23 '16 at 11:12
  • \$\begingroup\$ yes car collider on parent object then its first child contains bottom collider then its child contains righ collider and its sibling left collider. \$\endgroup\$ – Muhammad Faizan Khan Jun 23 '16 at 11:27
  • \$\begingroup\$ you mean that if i didn't added script to child collider then it will also affect the parent script who also have collider? \$\endgroup\$ – Muhammad Faizan Khan Jun 23 '16 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.