I am working on my own version of the nightmares project at the moment and encountered an odd problem.
So I want to improve upon the existing zombunny enemy with a vision filed so that he only starts running towards the player if the player is in his field of vision. I’m trying to base my solution upon the CCTV cameras from the stealth project in a way that the enemy has a TriggerCollider in front of him to determine whether the enemy can actually see the player or not.
Since the Zombunny still has another sphere collider (trigger) as an attack range attached to the parent object I wanted to attach a secondary vision field trigger as a child object to the player.
So I have the main enemy object with scripts for navigation, attacking and health attached. Also with a sphere trigger very close to the enemy so that it can attack if the player is close to it.
As a sub-object there’s a second “visionCone” object with the enemyVision script and another Trigger Collider attached.
If the player enters the trigger the code triggers as expected:
using UnityEngine;
using System.Collections;
public class EnemyVision : MonoBehaviour {
public Transform lookAtObject;
public float visibilityRange = 8f;
EnemyMovement enemyMove;
Ray playerVisibility;
void Start ()
{
enemyMove = GetComponentInParent<EnemyMovement>();
}
void OnTriggerStay (Collider other) {
if (other.tag == "Player")
{
enemyMove.chasePlayer = true;
}
}
}
… the enemy follows the player. So the trigger is working but the child object is apparently treated as a compound collider and also triggers the damage script on the main object.
Is there a way in which I can either prevent the viewRange Trigger from triggering the parent object scripts as well?
Or alternatively: Can I somehow restrict OnTrigger functions to a specific triggerzone on my GameObject?