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

How to allow player to kill enemy within a certain range.

$
0
0
I am making a 2D infinite runner game and i have my player working fine and my enemies working fine. At the moment my enemies are setup to die if the player presses the "a" key. But using the code below in c sharp the player can kill the enemies as long as they are on screen. I want to change this so that he'll only be able to kill them when he's close to them..ie. the "a" key will only work within a certain range of the enemy. Please Help! This is my enemy script so far: using UnityEngine; using System.Collections; public class EnemyScript : MonoBehaviour { HUDScript hud; void Update () { if(Input.GetKeyDown("a")) { Destroy(this.gameObject); } } void OnTriggerEnter2D (Collider2D other) { if(other.tag == "Player") { hud = GameObject.Find("Main Camera").GetComponent(); hud.DecreaseLives(1); } } }

Viewing all articles
Browse latest Browse all 1488

Trending Articles



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