my enemy recognizes one instance of the player entering a box collider in order to apply damage but not to trigger it to chase the player
i'm working on my ai script for a game i'm working on, and i need the enemy to chase the player until it's within range of the player to launch it's attack i have 2 enemy scripts, once that controls the enemies general functions including applying damage when the player enters the enemy box collider and another script that tells the enemy to begin chasing the player when the player enters the same box collider, the damage part works, but the chasing part has no effect whatsoever. Any help would be greatly appreciated.
using UnityEngine; using System.Collections;
public class EnemyAI : MonoBehaviour {
public float enemySpeed;
Animator enemyAnimator;
public GameObject enemyGraphic;
bool canFlip = true;
bool facingRight = false;
float flipTime = 5f;
float nextFlipChance = 0f;
public float chargeTime;
float startChargeTime;
bool moving;
Rigidbody2D enemyRB;
void Start(){
enemyAnimator = GetComponent<Animator> ();
enemyRB = GetComponent<Rigidbody2D> ();
}
void Update(){
if (Time.time > nextFlipChance) {
if (Random.Range (0, 10) >= 5) {
flipFacing ();
}
nextFlipChance = Time.time + flipTime;
}
}
void OnTriggerEnter2d(Collider2D Other){
if (Other.tag == "Player") {
if (facingRight && Other.transform.position.x < transform.position.x) {
flipFacing ();
} else if (!facingRight && Other.transform.position.x > transform.position.x) {
flipFacing ();
}
canFlip = false;
moving = true;
startChargeTime = Time.time + chargeTime;
}
}
void OnTriggerStay2d(Collider2D Other){
if (Other.tag == "Player") {
if (startChargeTime < Time.time) {
if (!facingRight) {
enemyRB.AddForce (new Vector2 (-1, 0) * enemySpeed);
} else {
enemyRB.AddForce (new Vector2 (1, 0) * enemySpeed);
}
enemyAnimator.SetBool ("Moving", moving);
}
}
}
void OnTriggerExit2d(Collider2D Other){
if (Other.tag == "Player") {
canFlip = true;
moving = false;
enemyRB.velocity = new Vector2 (0f, 0f);
enemyAnimator.SetBool ("Moving", moving);
}
}
void flipFacing(){
if (!canFlip) {
return;
}
float facingX = enemyGraphic.transform.localScale.x;
facingX *= -1f;
enemyGraphic.transform.localScale = new Vector3(facingX, enemyGraphic.transform.localScale.y, enemyGraphic.transform.localScale.z);
facingRight = !facingRight;
}
}
using UnityEngine; using System.Collections;
public class Enemy : MonoBehaviour { public float moveSpeed = 4f; // enemy move speed when moving public int damageAmount = 10; // probably deal a lot of damage to kill player immediately private EvolveController evolveAccessor;
public AudioClip attackSFX;
// private variables below
// store references to components on the gameObject
Transform _transform;
Rigidbody2D _rigidbody;
Animator _animator;
AudioSource _audio;
// movement tracking
[SerializeField]
int _myWaypointIndex = 0; // used as index for My_Waypoints
float _moveTime;
float _vx = 0f;
bool _moving = true;
// store the layer number the enemy is on (setup in Awake)
int _enemyLayer;
void Awake() {
// get a reference to the components we are going to be changing and store a reference for efficiency purposes
_transform = GetComponent<Transform> ();
_rigidbody = GetComponent<Rigidbody2D> ();
if (_rigidbody==null) // if Rigidbody is missing
Debug.LogError("Rigidbody2D component missing from this gameobject");
_animator = GetComponent<Animator>();
if (_animator==null) // if Animator is missing
Debug.LogError("Animator component missing from this gameobject");
_audio = GetComponent<AudioSource> ();
if (_audio==null) { // if AudioSource is missing
Debug.LogWarning("AudioSource component missing from this gameobject. Adding one.");
// let's just add the AudioSource component dynamically
_audio = gameObject.AddComponent<AudioSource>();
}
if (stunnedCheck==null) {
Debug.LogError("stunnedCheck child gameobject needs to be setup on the enemy");
}
// setup moving defaults
_moveTime = 0f;
_moving = true;
// determine the enemies specified layer
_enemyLayer = this.gameObject.layer;
// determine the stunned enemy layer number
_stunnedLayer = LayerMask.NameToLayer(stunnedLayer);
// make sure collision are off between the playerLayer and the stunnedLayer
// which is where the enemy is placed while stunned
Physics2D.IgnoreLayerCollision(LayerMask.NameToLayer(playerLayer), _stunnedLayer, true);
}
// if not stunned then move the enemy when time is > _moveTime
void Update () {
if (!isStunned)
{
if (Time.time >= _moveTime) {
EnemyMovement();
} else {
_animator.SetBool("Moving", false);
}
}
}
// flip the enemy to face torward the direction he is moving in
void Flip(float _vx) {
// get the current scale
Vector3 localScale = _transform.localScale;
if ((_vx>0f)&&(localScale.x<0f))
localScale.x*=-1;
else if ((_vx<0f)&&(localScale.x>0f))
localScale.x*=-1;
// update the scale
_transform.localScale = localScale;
}
// Attack player
void OnTriggerEnter2D(Collider2D collision)
{
if ((collision.tag == "Player") && !isStunned)
{
CharacterController2D player = collision.gameObject.GetComponent<CharacterController2D>();
if (player.playerCanMove) {
// Make sure the enemy is facing the player on attack
Flip(collision.transform.position.x-_transform.position.x);
// attack sound
playSound(attackSFX);
// stop moving
_rigidbody.velocity = new Vector2(0, 0);
// apply damage to the player
player.ApplyDamage (damageAmount);
// stop to enjoy killing the player
_moveTime = Time.time + stunnedTime;
}
}
}
between these two scripts, i can't figure out why one registers the collision and one doesn't. thanks!
Answer by NoseKills · Jul 21, 2016 at 06:15 AM
Have you tested whether your OnTriggerStay2d
ever gets called? I don't think it does since you have spelled it wrong. The last 'D' should be capital just like in your void OnTriggerEnter2D
Your answer
Follow this Question
Related Questions
Enemy 2D check for collision 1 Answer
Enemy direction going crazy after collision 1 Answer
Making a Box Collider sword do Damage 0 Answers
How to enemy animation attack player 0 Answers