Hello,
Based on the previous example ( the “clean your room” example where we can add: “Did you find your homework?” and we get a bool value of true or false) i thought OnCollisionEnter() is a perfect example of return method where we answer the question: “Was there a collision?” What am I getting wrong?
Welcome to the community.
i thought OnCollisionEnter() is a perfect example of return method where we answer the question: “Was there a collision?”
OnCollisionEnter() is called when a collision has occurred. Specifically, it’s called when the collider/rigidbody has begun touching another collider/rigidbody (or when we enter that collision).
We don’t need to return true/false to answer “Was there a collision?”, because we already know the answer – this method will only be called when there has been a collision.
Note that OnCollisionEnter(Collision collision) takes a Collision as a parameter so we can get information about the collision taking place. We can then do something with that collision data, such as find out which gameobject we’re colliding with. e.g.
void OnCollisionEnter(Collision collision)
{
Debug.Log("The gameobject we collided with is " + collision.gameObject.name);
}
Since we don’t need to return anything from this method, we can just set a void return type.
Hope that helps!
Makes perfect sense, thank you. “this method will only be called when there has been a collision” - for some reason I was assuming it is like in Update() where we get info every frame.
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.