- Home /
Trigger collider information
I have 1 object with a collider with isTrigger true. How to detect the number of objects inside the collider and assign each object a variable?
What I want to achieve is, if 2 objects are inside the collider, find the nearest object and do action. If only 1 object, just do the action.
Answer by moghes · Jan 20, 2014 at 09:24 AM
I will explain you more what the guys were trying to tell you, I will use tags. assign a tag to your objects that will enter the trigger. Here i have assigned "guest".
using UnityEngine;
using System.Collections;
public class MyClass : MonoBehaviour
{
private int count = 0;
void Update ()
{
if (colliders.Count == 1)
Debug.Log ("Do action based on 1");
else if (colliders.Count == 2)
Debug.Log ("Find the nearest and do action for 2");
}
void OnTriggerEnter(Collider col)
{
if(col.transform.tag == "guest")
count++;
}
void OnTriggerExit(Collider col)
{
if(col.transform.tag == "guest")
count--;
}
}
Answer by ankush_Kushwaha · Jan 19, 2014 at 08:19 AM
you can track and keep the number or count of the objects present in your collider. You can declare a variable count
and in your OnTriggerEnter()
increment it by 1 and in your OnTriggerExit()
decrement it by 1. So now the value of count
will tell you the number of objects present inside the collider.
Hi, does OnTriggerEnter() still works when an object is already in the trigger, then another object enters, it will +1? How do I get the transform of the objects in the trigger? The parameter, Collider coll can only store 1 object triggered, but what about multiple objects?
Answer by robertbu · Jan 19, 2014 at 08:04 AM
First, non-collider trigger solutions are easier but less accurate for this task. For example, Physics.OverlapSphere(). But there is a way to do it with a trigger.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Bug2 : MonoBehaviour {
private List<Collider> colliders = new List<Collider>();
void Update () {
if (colliders.Count == 1) {
Debug.Log ("Do action based on 1");
}
else if (colliders.Count == 2) {
Debug.Log ("Find the nearest and do action for 2");
}
}
void OnTriggerStay(Collider col) {
colliders.Add (col);
}
void LateUpdate () {
colliders.Clear ();
}
}
At each frame, when you reach Update() 'colliders' will be a list of all the colliders found in the OnTriggerStay(). You can compare the two (or more) to see which is closest.
collider.Add is like in Array? you clear and add items? Thanks!
Hey, Thanks for the nice tip! I tried your code, but sometimes the List.count returns 0, or sometimes the count is almost double the actual count.
for my game the 20 characters are actively go in and out of a trigger(quite randomly and frequently) and I think frame skipping is occuring..
Your answer
Follow this Question
Related Questions
Triggering candle lights on with Colliders 1 Answer
Erratic trigger behavior, I can't find the fault. 1 Answer
Triggers Interacting with Triggers 0 Answers
I need help with triggers 1 Answer