- Home /
how to rotate towards a target with a navmesh componet
Hello, i need help for a problem i'm unable to solve. I am making an RPG game and my character use a navmesh to move. Now everythings is fine but when i click on an enemy, i want my character to rotate toward it smoothly but everything i try doesn't work, it always snap directly in direction of the enemy. Please Help me :o
Here is my code
if (Input.GetMouseButtonDown(0)) { Ray ray = cam.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit, 100)) { if (hit.collider.tag == selectableObject) { objectClicked = hit.collider.GetComponent(); RotateToClick(objectClicked.transform);
private void RotateToClick(Transform _target) { Vector3 relativePos = _target.position - transform.position; Quaternion rotation = Quaternion.LookRotation(relativePos); transform.rotation = rotation;
Your answer
![](https://koobas.hobune.stream/wayback/20220612174550im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Smooth Turning on Mesh Navigation 0 Answers
How can i make a boat transport? 1 Answer
Shade Object Flat 1 Answer