Reputation: 28
I have a box collider that should act like a view of the AI and i have a player that has a capsule collider and the game objects name is "Player". When i use the build it void that is OnCollisionEnter and exit (btw this didn't work even if i used the trigger versions of the voids) it just doesn't detect it. I tried checking if the player as a game Object collides with the vision collider with the "Equals()" command and that didn't work to. I have attached pictures and my whole AI script if someone knows how to fix this.
and the script
using System;
using UnityEngine;
using UnityEngine.AI;
namespace Demo.Scripts.Runtime
{
public class AI : MonoBehaviour
{
[Tab("Main")]
[Header("Zombie States")]
[SerializeField] public bool is_Chasing_Player;
[SerializeField, Range(0f, 10f)] public float speed = 5f;
[Tab("Navigation")]
[Header("NavMesh Stuff")]
[SerializeField] public NavMeshAgent agent;
[SerializeField] public Collider vision_Collider;
[Header("Player Location")]
[SerializeField] public Transform player;
[Tab("Animation")]
[Header("Head Animator")]
[SerializeField] public Animator animator;
[Header("Animator Controllers")]
[SerializeField] public RuntimeAnimatorController idle;
[SerializeField] public RuntimeAnimatorController walk;
[SerializeField] public RuntimeAnimatorController die;
private int count = 0;
private void Start()
{
animator.runtimeAnimatorController = idle;
agent = GetComponent<NavMeshAgent>();
animator = GetComponent<Animator>();
}
private void Update()
{
ZombieDie();
if (player != null && is_Chasing_Player)
{
if (count == 0)
{
animator.runtimeAnimatorController = walk;
count++;
}
agent.SetDestination(player.position);
agent.speed = speed;
}
if (!is_Chasing_Player && animator.runtimeAnimatorController != idle)
{
animator.runtimeAnimatorController = idle;
}
}
public void ZombieDie()
{
if (Input.GetKeyDown(KeyCode.I))
{
animator.runtimeAnimatorController = die;
agent.speed = 0;
speed = 0;
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.collider.CompareTag("Player"))
{
Debug.Log("Player entered vision collider");
}
}
private void OnCollisionExit(Collision collision)
{
if (collision.collider.CompareTag("Player"))
{
Debug.Log("Player exited vision collider");
}
}
}
}
I was trying to write a sentence in the console when the player collides with the vision collider.
Upvotes: 1
Views: 70
Reputation: 4073
use OnTriggerEnter instead of OnCollision when "isTrigger" is checked.
documentation says:
One must have Collider.isTrigger enabled, and contain a Rigidbody
So add a Rigidbody to your agent and mark it "isKinematic".
When neither of them has a Rigidbody, the Physics Engine doesn't expect them to move and therefore collide. If you move the objects by script (navmesh agent) a kinematic rigidbody is what you want.
Upvotes: 0