- Home /
Rotation from a hit.point to worlds forward vector
Hey how is it possible to rotate a sphere's hit point from raycast to the worlds Vector3.forward. I tried this:
Quaternion toHitRotation = Quaternion.FromToRotation(hit.normal, Vector3.forward);
mThisTransform.rotation = Quaternion.Slerp(mThisTransform.rotation, toHitRotation, Time.deltaTime * 1);
I thought this would create the proper Quaternion, but it does not produce my desired result.Anyone can help me out?
Answer by robertbu · Aug 08, 2014 at 04:23 PM
To calculate the end rotation, you need to multiple by the rotation of the object:
Quaternion toHitRotation = Quaternion.FromToRotation(hit.normal, Vector3.forward) * transform.rotation;
And you don't show it in your code, but this needs to get executed repeatedly in Update() or a Coroutine() over a number of frames. Once you've calculated 'toHitRotation', you don't need to calculate it again.
mThisTransform.rotation = Quaternion.Slerp(mThisTransform.rotation, toHitRotation, Time.deltaTime * 1);
You may want to start with just assigning the rotation:
mThisTransform.rotation = toHitRotation;
Then figure out the Slerp() as a second step.
Yes it was the toHitQuaternion that was Update method causing the trouble along with multiplying with the object.Thanks!