I have a feeling this has been asked, but I’ve been through these forums a few times and tried a few things, but have had no luck so far.
So I have a character who uses the navMesh in unity pro to move to a position. That’s fine. Once he gets there, I want to give him a command to face a specific direction (locking z so he doesn’t end up leaning back in the world).
I started with transform.LookAt which functionally did what I want, but it’s a quick snap to and not what I want to have in the end.
So then I tried the following snippet:
(targetFacing is the object to face to)
float dx = transform.position.x - targetFacing.transform.position.x;
float dz = transform.position.z - targetFacing.transform.position.z;
float radians = Mathf.Atan2(dz, dx);
float angle = radians * 180 / Mathf.PI;
float rotateY = Mathf.LerpAngle(transform.position.y, angle, Time.time/2);
transform.eulerAngles = new Vector3(targetFacing.transform.rotation.x, rotateY, targetFacing.transform.rotation.z);
if((rotateY + transform.eulerAngles.y) >= 180){
Debug.Log ("Facing");
targetFacing = null;
}
The problem with this was depending on where targetFacing was in relation to his stop point, he would 3 out of 4 times face the target, but would sometimes face away, or at a 90 degree angle.
For example, if the target was north, south or east, he would face it, but if it was west, he would face away. I played with the angle to be radians * 180/Mathf.PI+90, -90, +180, etc but it always ended up with him facing it most of the time, but on occassion not.
So then I tried this:
strength being a float.
Quaternion targetRotation = Quaternion.LookRotation(targetFacing.transform.position - this.transform.position);
strength = Mathf.Min(strength * Time.deltaTime, 1);
this.transform.rotation = Quaternion.Lerp(this.transform.rotation, targetRotation, strength);
In a function called on LateUpdate (controlled via a bool), but that doesn’t seem to do anything at all.
I’m pulling my hair out on this one, can anyone help?