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

Rotating enemy object to face the player when moving

$
0
0
Hello i have a small problem. I am trying to write my own Ai logic what I am trying to do is to make the enemy to face the player when he is moving towards him. I have tried using the transform.LookAt function but it makes the enemy to move away from the player. I don't know how to make the enemy face the player. Can anybody help me ? public float movementSpeed = 6f; public float distanceFromTarget = 3f; Transform player; // Use this for initialization void Start () { player = GameObject.FindGameObjectWithTag("Player").transform; } // Update is called once per frame void Update () { Vector3 distance = player.position - transform.position; Vector3 direction = distance.normalized; Vector3 velocity = direction * movementSpeed; float distanceToTarget = distance.magnitude; if(distanceToTarget > distanceFromTarget) { transform.Translate(velocity * 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>