Question by
Cosmic_Owl130 · Jun 07, 2021 at 11:12 PM ·
ontriggerenteroncollisionenter
Collision not getting detected between 2 colliders
So I have a navmesh agent I used on a chicken model to get it to run away from a mouseclick, but for some reason, its collider does not detect any collision with any other colliders. I've tried using OnCollisionEnter as well as OnTriggerEnter, but still nothing. Here's my syntax:
public LayerMask clickOn;
private NavMeshAgent myAgent;
RaycastHit hitInfo;
public float fleeDistance;
void Start()
{
myAgent = GetComponent<NavMeshAgent>();
}
void Update()
{
if (Input.GetMouseButton(0))
{
Ray myRay = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(myRay, out hitInfo, 100, clickOn))
{
runFrom();
}
}
}
private void runFrom()
{
float distance = Vector3.Distance(transform.position, hitInfo.point);
if(distance < fleeDistance)
{
Vector3 dirToClick = transform.position - hitInfo.point;
Vector3 newPos = transform.position + dirToClick;
myAgent.SetDestination(newPos);
}
}
void OnCollisionEnter(Collision collision)
{
Debug.Log("hit");
Destroy(gameObject);
}
Comment
Best Answer
Answer by Cosmic_Owl130 · Jun 11, 2021 at 03:38 PM
Ok, so turns out I needed a rigidbody on the chickens as well
Your answer

Follow this Question
Related Questions
OnTriggerEnter happens too late 0 Answers
Destroy object on hover? 1 Answer
I'm using a Trigger for a projectile and I'm having trouble getting everything to work properly 0 Answers
How do I make this script play a particle sytem from prefab 1 Answer
End Game (Stop spawning) with OnTriggerEnter or OnCollisionEnter? 0 Answers