- Home /
OnTrigger does not work for multi targets
Hello. I am new to Unity. I am using Easy Ar multi-target tracker, and OnTriggerEnter does not work for me. I considered checking IsTrigger and having a rigid body. It is surprising that for a simple object like cube the OntriggerEnter works but for my multi-target, the OnTriggerEnter never called.
here is link of my project:
Pls, help me.
p.s. There may be a problem with the targets object like their shader directory, but I am not sure. When I move the character toward targets by tag, it moves but OnTriggerEnter does not work.
More Descriptions are as follows:
This is one of my targets tracked by an imageTracker.
And this is my character. both have rigid body , collider, and checked isTrigger.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class moveToTarget : MonoBehaviour
{
private float step;
private GameObject[] tag1;
private GameObject[] tag2;
private bool hit1;
private bool hit2;
// private bool hit3;
// Start is called before the first frame update
void Start()
{
hit1 = hit2 = false;
step = 2f * Time.deltaTime;
tag1 = GameObject.FindGameObjectsWithTag("tag1");
tag2 = GameObject.FindGameObjectsWithTag("tag2");
}
private void OnTriggerStay(Collider other)
{
hit1 = true;
}
void OnCollisionEnter(Collision collision)
{
hit1 = true;
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "tag1")
hit1 = true;
else if (other.gameObject.tag == "tag2")
hit2 = true;
// else if (other.gameObject.tag == "tag3")
// hit3 = true;
}
// Update is called once per frame
void Update()
{
if (!hit2)
transform.position = Vector3.MoveTowards(transform.position, tag2[0].transform.position, step);
else if (!hit1)
transform.position = Vector3.MoveTowards(transform.position, tag1[0].transform.position, step);
}
}
This is the script added to my character. The character movetoward target I mentioned, but the OnTriggerEnter never called.
sorry, but I need more details, code, and or images of the problem to help more.
Answer by Laiken · Feb 22, 2019 at 06:15 AM
Not sure I understood the problem, but make sure the object entering the trigger has a collider.
The problem is OnTriggerEnter never is called when I put the breakpoint.