transform.rotation spazes out
Hello,
I'm currently working on trying to create a 2D terrain hugging system for my game. I'm using a raycast to try and adjust the rotation of the player. For the most part it appears to work, the issue is that the player spazes out shaking madly.
In my debug log the raycast appears to sometimes be hitting multiple points at the same time or quickly changing the raycast collision point very slightly by like .00001 but very rapidly. Though when I look at the player Z rotation it appears to change between 0 and the correct rotation.
Here is the function that changes the rotation.
void Rotation(RaycastHit2D rayhit, float maxRotationDegrees)
{
Vector3 raycastNormal = rayhit.normal;
Quaternion targetRotation = Quaternion.FromToRotation(Vector3.up, raycastNormal);
Quaternion finalRotation = Quaternion.RotateTowards(transform.rotation, targetRotation,
maxRotationDegrees);
transform.rotation = Quaternion.Euler(0f, 0f, finalRotation.eulerAngles.z);
}
I've been racking my brain at this for hours, any help is greatly appreciated.
Your answer
Follow this Question
Related Questions
Mirror rotation of object along 2 Euler Axis' 0 Answers
Rotating an Object (To Face Another Object) Only on X and Y Axis 3 Answers
Need help aiming at new world space co-ordinates for gun recoil/spray pattern 0 Answers
Making a portal inspired game - Need help! 0 Answers
How to clamp rotation, following Unity's quaternion rules 1 Answer