- Home /
Is there a fix for ground normals rotates my player in unwanted ways?
I'm trying my hand at a Sonic Fan game, something I've wanted to do for years now, but I'm having issues with rotating my character based on the normals of the ground.
Before I say anything, I will clarify that I have rotated everything so that +Z is Upward, -Z is Downward, -Y is Forward (imagine the Y-axis is me pointing a finger at my screen), and +Y is Outward. Also, I'm using a 2D sprite in a 3D space.
In my testing I have found that, for whatever reason, rotating Vector3.up towards the normal where my raycast hits, rotates it almost in the way I want, but it is also changing my X rotation in the inspector to 45 degrees (it needs to be 90, otherwise my sprite will look squished as seen in the screenshots bellow). Is there a way to make sure that the X rotation in the inspector is ALWAYS locked to 90 degrees, making sure there is no possible way for that to change?
Here is the the code that handles this.
int layerMask = 0; layerMask = ~layerMask;
RaycastHit hit;
if (Physics.Raycast(rb.position, rb.transform.TransformDirection(Vector3.down), out hit, 0.5f, layerMask))
{
Quaternion temp = Quaternion.FromToRotation(Vector3.up, hit.normal);
onGround = true;
transform.rotation = Quaternion.Slerp(transform.rotation, temp, Time.deltaTime * 10f);
}
else
{
onGround = false;
transform.rotation = Quaternion.FromToRotation(Vector3.up, new Vector3(0, 0, 1));
}
[1]: /storage/temp/150826-screenshot-38.png
[2]: /storage/temp/150826-screenshot-39.png
Out of curiosity, why did you rotate everything so Z is up?
Should your character be able to turn and run along the Y axis? (even though the camera always shows his side)
If not, could you try this: { Vector3 normal$$anonymous$$odified = new Vector3(hit.normal.x, 0, hit.normal.z); Quaternion temp = Quaternion.FromToRotation(Vector3.up, normal$$anonymous$$odified); onGround = true; transform.rotation = Quaternion.Slerp(transform.rotation, temp, Time.deltaTime * 10f); }