Hello,
Im trying to make an object follow the center of the camera and the script essentally works.
The issue I am having is that while the object follows the point, it will flip at the axis constraints due to the Quaternions.
Does anyone have any idea how to prevent these flips?
I looked into using eulerAngles but they seemed rather over my head.
My script is included below for refrence.
Thanks for any assistance!
var target : Transform;
function FixedUpdate () {
var ray = camera.ScreenPointToRay (Vector3((Screen.width /2),(Screen.height /2),0));
Debug.DrawRay (ray.origin, ray.direction * 10, Color.yellow);
var relativePos = ray.GetPoint (10);
var rotation = Quaternion.LookRotation(relativePos - target.transform.position);
target.transform.LookAt(ray.GetPoint(10));
target.position = Vector3.Lerp(target.position, ray.GetPoint(5), Time.deltaTime * 1);
Quaternion.Slerp (target.rotation, transform.rotation, Time.deltaTime * 1)
Quaternion.LookRotation has an optional second parameter to specify which direction is up. The function will try to get the up direction as close as possible to the one you specify, but it may suddenly flip the rotation if the rotation goes too far.
I’ll be honest, I’m not sure exactly what your script is doing, but it may be that the value of 10 that you use for ray.GetPoint is the distance of your object from the camera. If so, try using a value slightly less than this (9.5, maybe). It might give the quaternion calculation a little bit of room to play with.
Thanks for the advice andeeee, I managed to find a way round it in the end but I appreciate you taking the time to offer advice.
Thanks again.
Mark
Mark,
Were you able to just work around the flipping with additional scripting, or is it a Quaternion issue/factor?
Same issue, but I didnt find fixed.
Posting ambiguous comments to an over-ten-year-old question is not a good Life Strategy.
How to report problems productively in the Unity3D forums:
http://plbm.com/?p=220
1 Like
İn my opinion that’s true but it sentence mean is I don find still.