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
↧