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

How To Make NPC Follow Player When In Range?

$
0
0
I am trying to make a hostile NPC follow and attack my player when the player is in range of the NPC. Currently, the NPC's with the following script attached start following my player the instant I start the game, even when they are very far away. I want to change that. My script: if(attack.rigidbody2D.transform.position.y > (rigidbody2D.transform.position.y + distance)){ rigidbody2D.transform.position += Vector3.up * speed * Time.deltaTime; } if(attack.rigidbody2D.transform.position.y < (rigidbody2D.transform.position.y - distance)){ rigidbody2D.transform.position += Vector3.down * speed * Time.deltaTime; } if(attack.rigidbody2D.transform.position.x > (rigidbody2D.transform.position.x + distance)){ rigidbody2D.transform.position += Vector3.right * speed * Time.deltaTime; } if(attack.rigidbody2D.transform.position.x < (rigidbody2D.transform.position.x - distance)){ rigidbody2D.transform.position += Vector3.left * speed * Time.deltaTime; }

Viewing all articles
Browse latest Browse all 1488

Trending Articles



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