Sorry for a long question. I am fairly new to Unity. I tried to search around the answer to the question but couldn’t find any. There were granted posts which were similar. Below is the fairly simple code I wrote. If the Obstacle does not have a RigidBody then the else if is not satisfied, unless isOnGround = false. However once I add a RigidBody then the code below works even if isOnGround = true. What is the logic behind this?
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
isOnGround = true;
}
else if (collision.gameObject.CompareTag("Obstacle"))
{
gameOver = true;
Debug.Log("Game Over");
print("Game Over");
}
}
are you trying to check if it has a rigidbody because if you are then use collision.gameObject.TryGetComponent(out RigidBody rb)
@Tomatotom1234 thanks for your reply. this one is from the basic programmer tutorials of unity. In the tutorial they want to detect the collision with an object which has a tag “obstacle” and if satisfied then the game should be over. The problem (or more due to lack of my understanding) is that without any rigidbody of obstacle, player object recognizes the ground collider and if that is satisfied then it does not recognize the obstacle collision. However when the rigidbody is added to obstacle, then the player object recognizes the collision with ground and obstacle colliders.
Effectively what I want to get at is that can a player recognize the collision with two colliders (that are part of objects with different tags ) without any one of them (i.e. ground and obstacle in this case) having a rigidbody?