its been a long time since ive done any programing and im having trouble with this small task
i just want to change the fpc script to walk towards a target object instead of using player input, but i cant figure out how to tell it which way it should be facing, i tried using look at but that just snaps the rotation to the targets direction instantly
what formula do i use to make var A contain the rotation needed to face the target object
You subtract the target’s position vector from “your” position vector. That creates a direction vector pointing from this pos to the target. The magnitude of that direction vector is the distance to target.
Then you would use the Quaternion.LookRotation function to create a target rotation, then lerp from the current rotation to the target rotation smoothly.
C#
//how quickly we turn towards the target
float rotationSpeed = 0.5F;
Vector3 dir = target.transform.position - transform.position;
Quaternion targetRotation = Quaternion.LookRotation(dir.normalized);
float str = Mathf.Min(rotationSpeed * Time.deltaTime, 1);
transform.rotation = Quaternion.Lerp (transform.rotation, targetRotation, str);
I don’t know if you’d have to do anything else to get the FPC to move, but this is how you figure out how to move.
The Quaternion.LookRotation takes the selected up vector as the second parameter, which is currently positive Y axis (0, 1, 0, or Vector3.up). You should be able to just change what direction it uses for up. I haven’t tried it myself though, never needed to.
One thing that isn’t clear, do you want your object’s x rotation to match the target’s x rotation, or do you want you object to face in the direction to your other object, but pointing a different axis towards it? If it’s the latter, try what I said above, otherwise just modify the object’s x rotation to match the target’s.