Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

Make enemy move away from player when hit.

$
0
0
For the past day+ Ive been working on attacking an enemy that does 1 damage. On swing a trigger is created and when the enemy touches the trigger it takes 1 damage. Problem is the enemy keeps moving while knocked back and the trigger keeps ticking damage. Ive tried having its movement set to 0 while hit or not grounded (from being knocked back in the air), but doesn't seem to affect it. When i started it was ticking 20 times a second but now I have it down to just 2-3 times a second. I was thinking it was because the enemy keeps moving towards the player after being hit. -Set to GetKeyDown for one time execution on player controller script, ive tested all that and its perfect so the problem is in the enemy script. 1. Would having the enemy run away solve this, if so how to? 2. How can I have the enemy only be hit once per swing? Seems Ive coded a million different methods and nothing works perfectly. **Enemy script in question** (I took out all code here except the problem code) void OnTriggerEnter2D(Collider2D other) { if (other.CompareTag ("PhysicalWep")) { if(other.transform.position.x < transform.position.x){ knockFromRight = true; }else {knockFromRight = false;} if (knockFromRight){ GetComponent().AddForce(new Vector2(force, force)); animation.Play("Ghost_RedFlash"); TakeDamage(); } if (!knockFromRight){ GetComponent().AddForce(new Vector2(-force, force)); animation.Play("Ghost_RedFlash"); TakeDamage(); } hurt = true; damage = 1; } } private void TakeDamage(){ CurrentHealth -= damage; Debug.Log(currentHealth); } void OnTriggerExit2D(Collider2D other) { if (other.CompareTag ("PhysicalWep")) { if(grounded) hurt = false; } } void FixedUpdate () { //--------Speed------- if (!grounded || hurt) mobspeed = 0f; if(grounded && !hurt) mobspeed = 2f; //--------HP---------- if (currentHealth > maxHealth){ currentHealth = maxHealth; } if(currentHealth <= 0){ Destroy(gameObject); }

Viewing all articles
Browse latest Browse all 1488

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>