- Home /
Reset slerp to.rotation to avoid follow through
Good day to you fellow humans,
Im a little puzzled by the following and wish to seek the assistance of someone cleverer than myself. I would like to reset a to.rotation after slerping to the current transform.rotation to avoid continuing the previous slerp the next time the code is called.
This is the relevant part, works fine:
if (Input.GetMouseButton (0)) {
rotationX += Input.GetAxis ("Mouse X") * cameraSensitivity * Time.deltaTime;
rotationY += Input.GetAxis ("Mouse Y") * cameraSensitivity * Time.deltaTime;
rotationY = Mathf.Clamp (rotationY, -90, 90);
Rotat = Quaternion.AngleAxis (rotationX, Vector3.up);
Rotat *= Quaternion.AngleAxis (rotationY, Vector3.left);
transform.rotation = Quaternion.Slerp (transform.rotation, Rotat, Time.deltaTime * LookDamping);
}
Now i would have thought that adding this...
if (Input.GetMouseButtonUp (0)) {
Rotat = transform.rotation;
}
...would stop the camera rolling on to the previous target rotation the next time the button is clicked, but it doesnt seem to do anything.
Any thoughts?
Well as you are only Slerping when the button is down it should stop moving anyway - something else must be rotating it!
Sorry obviously didnt make myself clear enough.
The problem is not a continuation of rotation on mouse button up (it does stop), the problem is the continuation of the previous rotation trajectory when the button is pressed again.
Therefore i am attempting to reset 'Rotat' to the current rotation.
Looks to me like you need to reset rotationX and rotationY to 0 then...
No that just springs the rotation to zero next time the button is pressed, as does setting rotationX/Y to transform.rotation.x/y, I recall now its one of the many things I have tried before resorting to asking here.
I suppose though the solution must lie in translating the current rotation back to the RotationX/Y variables. But transform.rotation is an angle, RotationX is some random float, is this possible?
Looks to me like it will be the eulerAngles of the current rotation x = -transform.rotation.eulerAngles.z and rotation y = transform.rotation.eulerAngles.y
Answer by User Banned · Nov 03, 2012 at 10:05 PM
Following code was the answer, got there in the end, though not sure i totally understand the logic of it.
if (Input.GetMouseButtonUp (0)) {
rotationX = transform.eulerAngles.y;
rotationY = (0 - transform.eulerAngles.x);
}
As you can see RotationX/Y are flipped from eulerangles.x/y, not sure why it should be like that but it works so hey. Thanks whydoidoit for prodding in the right direction.
It's flipped because you are using Vector3.left - I didn't even know that existed!!!!
The axes of rotation are forward, right and up.