0
\$\begingroup\$

I am trying to rotate a square GameObject, so it appears to be standing perpendicular to the slope underneath it. However, when above a slope, the square constantly "spins"; it seems like the rotation is set and then immediately set back to 0, causing it to jitter.

I am currently using a RayCast based character controller instead of rigidbody. To change the rotation, I added the follow line of code following the vertical RayCast:

if (_raycastHit)
    {
       transform.rotation = Quaternion.FromToRotation(transform.up, _raycastHit.normal) * transform.rotation; 

    ...
    }

It calculates and rotates the square correctly, but I am not sure why it resets back to 0 every other frame. To avoid taking up too much space, the character controller I am using is hosted on GitHub here. Specifically, this is in the moveVertically() method.

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

It looks like you might want something as simple as:

transform.rotation = Quaternion.LookRotation(Vector3.forward, _raycastHit.normal);

Or, if you rotate your character to flip them, you can use transform.forward instead of Vector3.forward.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .