Look perpendicularly to a collision point

Hello,

I’m working on a FPS-like game, featuring a capsule-shaped player and a map modeled with Blender.

When the player hits a wall of the map, I would like to rotate him so that the collision point is exactly on its left (or its right). In other words, anytime I hit a wall, I want my look to be parallel to the wall.

On the following schematic (seen from above), the red arrows shows the player movement, the blue arrows show the player’s forward axis before the collision, and the green arrows show the player’s forward axis I’d like to have after the collision.

[30825-sans+titre.png|30825]

Thanks a lot for your help!

Try this out!

void OnCollisionEnter(Collision collision){
	foreach(ContactPoint contact in collision.contacts){
		if(Mathf.Abs(contact.normal.y) < 0.05){ //the collision was approximately horizontal (a wall not the ground)
			Vector3 dirPerp = new Vector3(contact.normal.z, 0, -contact.normal.x); //find the perpendicular vector
			Vector3 newFwd = Vector3.Project(transform.forward, dirPerp);
			transform.forward = newFwd == Vector3.zero ? transform.right : newFwd;
		}
	}
}

Hope that helps,

Scribe