Checking what face an object collided with

How do you find what face of an object has been hit when OnCollisionEnter is called? I see there is a collision point.

How would I check what face of an object that is touching?

What exactly do you need? The index of the triangle of a mesh colider? And what do you need it for?

I have a cube in mid air that you can rotate. Different shaped objects fall from above and land on the cube. I want to find which face of the cube the objects are colliding with to see whether the player scores a point…

If its just about the cube you can do it with the surface normal you get form the collisions contact points. If the normal equals the cubes forward normal its the front, if it equals the inverse forward normal its the back. If it equals the cubes up normal its the top etc.

Wow that’s awesome! I didn’t even know what the normal was (noob lol). How would I find the surface normal? LIke I can find my way around code fairly well… But normals I’ve never tried really. Could you show me an example of how it’d work? Then even better then getting an answer I might actually learn (give a man a fish and all that),

Thanks! :slight_smile:

This is how it could be done I guess. the variable “cube” should be the transform of the cube the objects collide with.

//OnCollisionEnter in the script of the falling objects
public void OnCollisionEnter(Collision collision)
{
	Vector3 normal = collision.contacts[0].normal;
	
	switch(normal)
	{
	case cube.forward:
		//front
		break;
	case -(cube.forward):
		//back
		break;
	case cube.right:
		//right
		break;
	case -(cube.right):
		//left
		break;
	case cube.up:
		//top
		break;
	case -(cube.up):
		//bottom
		break;
	}
}

Awesome thanks so much!!

Assets/Code/MainTouchLogic.cs(154,5): error CS0151: A switch expression of type `UnityEngine.Vector3’ cannot be converted to an integral type, bool, char, string, enum or nullable type

I’m guessing that just means I can’t use a Vector3 conditional in a switch statement… So I need to figure out the collision contact before using the switch statement?

Ah, thats too bad. If switch doesnt work, simply use if statements like

if(normal == cube.forward)
{
	//front
}
else if(normal == -(cube.forward))
{
	//back
}
//and so on

Epic, just tested on device! Works perfect!! Woo! Thanks mate :slight_smile:

	void OnCollisionEnter(Collision collision)
	{
		Vector3 normal = collision.contacts[0].normal;
	
		if(collision.collider.GetComponent<FallingObject>().CheckAndDestroy())
		{
			ScorePoint();
		}
		
	    if(normal == transform.forward)
		{
			Debug.Log("WORKED FORWARD");
			ScriptPointsTxt.guiText.text = ("WORKED FORWARD");
		}
		
		else if(normal == -(transform.forward))
		{
			Debug.Log("WORKED BACKWARD");
			ScriptPointsTxt.guiText.text = ("WORKED BACKWARD");
		}
		
		else if(normal == transform.right)
		{
			Debug.Log("WORKED RIGHT");
			ScriptPointsTxt.guiText.text = ("WORKED RIGHT");
		}
		
		else if(normal == -(transform.forward))
		{
			Debug.Log("WORKED LEFT");
			ScriptPointsTxt.guiText.text = ("WORKED LEFT");
		}
		
		else if(normal == transform.up)
		{
			Debug.Log("CIRCLE");
			ScriptPointsTxt.guiText.text = ("HIT CIRCLE");
		}
		
		else if(normal == -(transform.up))
		{
			Debug.Log("WORKED DOWN");
			ScriptPointsTxt.guiText.text = ("WORKED DOWN");
		}
		else
		{
			ScriptPointsTxt.guiText.text = ("NO NORMAL");
			return;
		}
	}