Player Rotating Oddly When Aligning to a Certain Normal

I have a slide over mechanic in my game where the player can slide over objects. When the slideover is initiated, the player will immediately rotate on the y axis to face the surface and then slide over it. Its generally working, however on a wall facing a particular direction, the hitInfo retuns (0,0,1) which flips the player -180 on the x axis. I’m unsure why this is happening as the rotation should be on the y axis.
Any ideas on how to fix it?

//Create a ray forwards from the player to detect any surfaces
 ray = new Ray(new Vector3(transform.position.x, transform.position.y + 0.25f, transform.position.z), transform.forward);
RaycastHit hitInfo;
//Rotate the player to face the surface if one is detected
if (Physics.Raycast(ray, out hitInfo, 1, mask)) {
	transform.rotation = Quaternion.FromToRotation(Vector3.back, hitInfo.normal);
}

Use Quaternion.LookRotation

You are assigning your transform’s orientation as a rotation.

Quaternions are like vectors in that they can either be an absolute position in space or an offset. If you want your transform to move 1 unit to the right from its current you can write transform.position += Vector3.right which is of course different from setting its position to be equal to Vector3.right. The former uses it as an offset and the latter as an absolute value. When used as an absolute value, it’s essentially still an offset but from the origin point, (0,0,0).

The same is true of quaternions: they’re either rotations, like our vector offset, or they’re orientations, like our absolute position. When we assign a quaternion to our transform’s rotation field, it’s just saying this is the rotational change from a starting point of facing forward that this object needs to undergo. The FromToRotation function returns us a rotation, used to multiply an existing orientation in order for it to undergo the same rotational transform as required to get from the first direction argument you pass to FromToRotation to the second.

If you feel this is enough information to have another go then my final tip is that I feel as though your solution will either require Quaternion.LookRotation if you want to specify the direction in which your rotation is looking or will require a nice old Quaternion.AngleAxis if you want to explicitly specify the axis around which your transform rotates. I can give you more help if you can post a little more information about your problem, a screenshot might be useful just showing what axes of your object need to orient to which directions relating to the slide object.

Hope this is some help :slight_smile: