0
votes

I'm following this tutorial to create PONG in Unity 2d :-

http://unity.grogansoft.com/beginners-guide-create-pong-clone-in-unity-part-6/

And understand the code for the most part, but this section confuses me. I have highlighted the confusing part in bold. I can't see in any of the code examples where the name of the ball is being checked? What am I missing?

Code:

void OnCollisionExit2D(Collision2D other)
{
    float adjust = 5 * direction;
    other.rigidbody.velocity = new Vector2(other.rigidbody.velocity.x, other.rigidbody.velocity.y + adjust);        
}

We make sure the item hitting the paddle is the ball by checking its name, then we apply a force to its rigidbody in the direction of the paddle’s movement. This also has the pleasant side effect of adding a little extra speed to the ball, making it faster and faster as the game goes on.

1
I've looked through the whole tutorial up to this point (e.g. all the parts I've already done - there isn't much) and can't find this reference anywhere. I'm not sure if I'm just not understanding, or if this is missing. I mean, the tutorial will work, because the only thing that CAN collide with the paddle, is the ball, but that's beside the point, I'd still like to know how to do this properly lol. - JamesMatson
The 'OnCollisionExit2D' function doesn't need a reference to the ball in order to work. It'll apply to anything it comes into contact with, so long as it has a collider - that would include your ball, as well as anything else. - Sean Carey
Thanks, I understand that it technically would work in a Pong-style scenario, but the notion of checking which object is colliding is still relevent to me. I will eventually build something where I want different actions when different objects collide, so I'd like to know how to do this at some point, and thought the tutorial was meant to be showing me how. All good though, just misleading :) - JamesMatson

1 Answers

1
votes

I think you are correct in your thinking: they don't really "check the name". But, to clarify for you without really having gone through the tutorial, the code you quote appears to be the "Paddle" class ("PaddleScript"?).

The input parameter "other" is the ball--the only object that can strike the paddle.

So, their text is a bit misleading. Perhaps there was supposed to be another object floating around.