Quantcast
Channel: Latest Questions on Unity Answers
Viewing all articles
Browse latest Browse all 171066

How to make top-down objects rotate to face another?

$
0
0
As the title says, I am making a top down style game, and am having difficulty getting enemies to rotate to face the player properly. I found this code from another post, and have tried tweaking the values of Vector3.RotateTowards, but I can't quite get the result I'm looking for. public Transform target; public float speed; void Update() { Vector3 targetDir = target.position transform.position; float step = speed * Time.deltaTime; float moveStep = moveSpeed * Time.deltaTime; Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 0.0F); transform.rotation = Quaternion.LookRotation(newDir); With this code, the enemy will rotate on all 3 axes, but will eventually become 'invisible'. How do I isolate just the 'z' axis? Thanks! edit: I know this question has been brought up before, but I've looked through all the solutions I've found and haven't been able to find one that suits my needs.

Viewing all articles
Browse latest Browse all 171066

Trending Articles



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