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

Enemy follow player

$
0
0
Hi all im making a 2D platform game, i have this script where the enemy follow the player at certain distance, but i realise that the enemy can cross the walls,c an some helpme ? void Update () { if (life <= 0) { Death(); } range = Vector2.Distance(transform.position, player.position); Debug.DrawLine(player.position, enemy.position, Color.red); if(range < distance) { transform.position = Vector2.MoveTowards(transform.position, player.position, moveSpeed * Time.deltaTime); anim.Play("Walk"); } if(range > distance) { anim.Play("Idle"); } if(player.position.x > transform.position.x){ //face right transform.localScale = new Vector3(1,1,1); }else if(player.position.x < transform.position.x){ //face left transform.localScale = new Vector3(-1,1,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>