- Home /
Question by
UnityProjec · Feb 18, 2020 at 01:48 AM ·
aidamagehealth
AI keeps attacking objects that are not player.
void Update()
{
transform.LookAt(thePlayer.transform);
if (attackTrigger == false)
{
enemySpeed = 0.01f;
theEnemy.GetComponent<Animation>().Play("walk");
transform.position = Vector3.MoveTowards(transform.position, thePlayer.transform.position, enemySpeed);
}
if (attackTrigger == true && isAttacking == false)
{
enemySpeed = 0;
theEnemy.GetComponent<Animation>().Play("attack");
StartCoroutine(InflictDamage());
}
}
void OnTriggerEnter()
{
attackTrigger = true;
}
void OnTriggerExit()
{
attackTrigger = false;
}
The AI keeps attacking objects and they will attack ramps whenever they go up them, causing the player damage. Is there a reason to solving this?
Comment
Best Answer
Answer by tormentoarmagedoom · Feb 18, 2020 at 02:35 AM
You need to check what has activated the TRrigger.
https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html
void OnTriggerEnter(Collider other)
{
if (other.gameObject==thePlayer)
{
attackTrigger = true;
}
}
So only activates it if is the player, Now is triggering in all other colliders!
Your answer
Follow this Question
Related Questions
Why Wont My AI Deal Damage To My FPS On Collision 0 Answers
The enemy don´t lose health, but why??? 0 Answers
Ai that applies damage in collision? 1 Answer
Ai that applies damage when in range? 1 Answer
Deal damage on collision 2 Answers