Don't count as hitting obstacle if landing on it nicely

I made a multi-part rocket at the beginning of the course that includes parts called Rocket Fin Stand 1-4 that are the parts of the rocket that actually touch the ground. As I was going through and adding collision it didn’t seem fair to count the rocket landing nicely on an obstacle as a bad collision so I tried to figure out a way to get it to ignore the collision if it’s only that part of the rocket that touches an obstacle.

I can’t guarantee that this is the best way to do it, and I won’t pretend I 100% understand the code I found online, but I got it working! Now when my rocket collides with an obstacle it’ll check which part of the rocket touched the obstacle and if the name contains “Rocket Fin Stand” then it doesn’t count it as a true collision.

	private void OnCollisionEnter(Collision collision) {
		switch (collision.gameObject.tag) {
			case "Friendly":
				Debug.Log($@"{collision.gameObject.name} is friendly.");
				break;
			case "Finish":
				Debug.Log($@"You finished the level!");
				break;
			default:
				if (collision.GetContact(0).thisCollider.transform.gameObject.name.Contains("Rocket Fin Stand") == false) {
					Debug.Log($@"{collision.gameObject.name} is an obstacle and you suck for hitting it.");
				}
				break;
		}
	}

One way to do this would be having a separate collider around each section of the rocket you want to handle collisions for. That way you can detect them individually and handle the collisions in separate scripts.

1 Like

Privacy & Terms