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

Enemy stops patrolling when player comes close

$
0
0
Hey guys! I am pretty new to Unity and C# so I wanted to ask how it is possible to make my enemy stop moving when the player comes close. My enemy moves this way: if (moveRight) { transform.Translate(2 * Time.deltaTime * speed, 0, 0); transform.localScale = new Vector2(1, 1); } else { transform.Translate(-2 * Time.deltaTime * speed, 0, 0); transform.localScale = new Vector2(-1, 1); } if (trig.gameObject.CompareTag("stopper")) { if (moveRight) { moveRight = false; } else { moveRight = true; The trigger is there because I created two GameObjects my enemy collides to so that he turns. now I want to make him stop when the player comes close. I found a solution like this: Vector2.Distance(transform.position, player.position but it didn't to the job. I hope you can help me out :) Kind regards

Viewing all articles
Browse latest Browse all 1488

Trending Articles



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