Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by mansdferg · Jul 21, 2016 at 12:55 AM · aienemyenemy aiattackfollow player

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

92 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiate Bullet that points to the player 0 Answers

My enemy ai won't stay on the ground help please 0 Answers

My enemy attacks backwards, and i'm not sure why it does this 0 Answers

How can I make an enemy follow the player when the player is not looking at the enemy? 0 Answers

Enemy 2D check for collision 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges