- Home /
why does collider data-type null itself?
i have a game-object (enemy = E) and two player-objects (P1 & P2). when leftclicking on a player-object and then rightclicking on a location on the ground-plane in the scene a marker is placed and the player moves to the set position,
E has a sphere-collider marked as trigger on it. when a player-object enters the trigger, the collider data-type is stored and the entered player becomes a target for E to focus on, i've tried to set it up in such a way that if P1 enters the zone first, then E will ignore P2 until P1 is dead, or leaves using a on-trigger exit.
now here comes my strange experience, if P1 enters the zone, and then collides with the box-collider that E has, then the collider-data that lets E locate P1 becomes null,
the problem with this is esentialyy that it would the player to simply ram their enemies to imobilize them, a cool ability but not something that should be a default feature.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class EnemyController : MonoBehaviour
{
public float MeleeRange = 5;
public float AttackStrength = 15;
public float EnemyShield = 50;
public float EnemyHealth = 400;
private Collider EnemyTarget;
public NavMeshAgent Agent;
private Vector3 EnemyTargetDestination;
private void Update()
{
if(EnemyTarget != null)
{
EnemyTargetDestination = EnemyTarget.transform.position;
SetTargetAsDestination();
}
else
{
Debug.Log("no target");
}
if(EnemyHealth <= 0f)
{
Destroy(gameObject);
}
}
private void OnTriggerEnter(Collider other)
{
if (EnemyTarget == null)
{
if (other.tag == "Player")
{
Debug.Log("player entered range");
EnemyTarget = other;
}
}
}
private void OnTriggerExit(Collider other)
{
if(EnemyTarget.gameObject.name == other.gameObject.name)
{
EnemyTarget = null;
EnemyTargetDestination = transform.position;
}
}
private void SetTargetAsDestination()
{
if (Vector3.Distance(transform.position, EnemyTargetDestination) >= MeleeRange)
{
Agent.SetDestination(EnemyTargetDestination);
Agent.isStopped = false;
}
if (Vector3.Distance(transform.position, EnemyTargetDestination) <= MeleeRange)
{
Agent.SetDestination(transform.position);
DamageTarget();
}
transform.LookAt(EnemyTargetDestination);
}
private void DamageTarget()
{
EnemyTarget.GetComponent<StatController>().TakeDamage(AttackStrength * Time.deltaTime);
Debug.Log("Target health = " + EnemyTarget.GetComponent<StatController>().CharacterHealth);
}
public void TakeDamage(float damage)
{
EnemyHealth = EnemyHealth - (damage / ((EnemyShield + 100) / 100));
}
}
upon collision between P1 and E, P1 can step away by a unit or so, and then E.target == null, if P1 and E then come into contact again, P1 will become the target of E as long as they touch, and then E.target becomes null if P1 steps away again....
here is some screen-capture of the behaviour.
EDIT: I've tried the same thing, but disabling the smaller box-collider, the box-collider does not affect the situation.
Answer by KreativeDoggo · Oct 18, 2019 at 03:56 PM
Problem have been solved, there was a child-object wiht a collider marked as trigger. this would react to the onTrigger functions.
Your answer
