Weird Rotation Error

Updated on July 9, 2019 in [A] C# .Net
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
3 on July 8, 2019

I have code that rotates the character to match the normals of walls (matches the walls rotation). This works perfectly until my char turns around 180°. Then instead of rotating Left & Right, it rotates like a windmill.


I believe tht the specific piece of code causing this problem is the transform.rotation within the RotateTowards() function.

if (avgNormal !=
    // Calculate new char rotation to match/be parralel with wall rotation/normals.
    Quaternion charWallRotation = Quaternion.FromToRotation(Vector3.forward,-(avgNormal / numNormals) * Time.deltaTime);
    // Rotate char smoothly to match normals of wall if moving left/right.
    if (Input.GetAxis("Horizontal") != 0)
        transform.rotation = Quaternion.RotateTowards(transform.rotation, charWallRotation, (wallRotationSpeed * 100) * Time.deltaTime);

The Debug.Log of the transform.rotation on both the working wall pressing the sideways movement keys.

  • Liked by
0 on July 8, 2019

Update: I’m confident that it’s to do with charWallRotation’s FromToRotation() method as playing with it flips my char upside down and I can move around like normal (but move directions are reverse as it’s flipped upside-down). Any suggestions would be greatly appreciated.

  • Liked by
1 on July 8, 2019

Have you tried using transform.Rotate() instead of directly setting the rotation?

Quaternions can do weird things when you directly set them.

on July 9, 2019

It wasn’t that exact function but you were right about directly changing the rotation messing things up. And that managed to get me onto the right direction. I stupidly hadn’t checked the documentation for RotateTowards() which happened to have the solution.Thank you very much!

Show more replies
  • Liked by