- Home /
Nested if statement only runs once. I don't know why.
Hi, I'm rather new to programming and unity. I've run into a problem that I think should be really simple to fix but I just don't know how.
I've been trying to make a simple pong game. Currently working on trying to create a system so that when the ball hits certain area of the paddle the ball will get a force added to it which will make the ball bounce with some "english" on it.
So I thought I would accomplish this by making a comparison between the ball's x coord and the paddle's on collision. the paddle is 3 wide, if the ball hits while it's x coord is greater than -1 but less than 0 of of the paddle's x coord, the game should recognize the ball as hitting the left side of the paddle and apply some force to the ball so that it will have "english".
Whenever I run this script my statement print("hit the left zone");
in the nested if statement only shows up in my console once per launch of the game, no matter how many times i hit the ball on the left side of the paddle.
Can somebody help me find out why I am only getting 1 instance of print("hit the left zone");
in my Debug log. I find this strange because I can get infinite instances of Debug.Log("Ball Hit South Paddle!");
to show up in my debug console.
I am just trying to make sure the ball is being recognized as hitting the correct part of the paddle so that I can move on to figuring out what type of physics interaction I want.
Here is my collision function `
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "south_paddle")
{
Debug.Log("Ball Hit South Paddle!");
//trying to find out if the ball hits the left side of the paddle
if (ballPos.x >= south_paddle.south_paddlePos.x - 1 && ballPos.x <= south_paddle.south_paddlePos.x)
{
print("hit the left zone");
}
}
thanks for reading
EDIT-------
allright, so I tried all of the suggestions that were mentioned here.
I changed out that random print, to Debug.Log.
I Included parentheses in the nested "if" to better define it
I tried OnCollisionStay2D, no debug reports
So, I'm still at a bit of a loss here. I'm not sure how can make my game report its recognition of the ball hitting the left side of the paddle.
I do appreciate everyone's suggestions though, I thought at least one of them might work, but sadly not.
Why are you using Debug.Log in one case and print in the other? And, have you tried simply putting a breakpoint on the first Debug.Log, and then stepping through the code to see what it's doing?
Or at leasts include the values of ballPos.x and south_paddle.south_paddlePos.x in your first Debug.Log() call. :)
I FIGURED IT OUT!, basically I'm an idiot, I put my definition for the balls' position in my Start function, thus locking it's value at the starting position. Now It's in FixedUpdate and everything is right with the world.
Answer by MrSoad · Oct 13, 2014 at 10:54 PM
Could be because you have not put brackets around the two arguments either side of the && Try this :
void OnCollisionEnter2D(Collision2D col) {
if (col.gameObject.tag == "south_paddle") {
Debug.Log("Ball Hit South Paddle!");
//trying to find out if the ball hits the left side of the paddle
if ((ballPos.x >= south_paddle.south_paddlePos.x - 1) &&
(ballPos.x <= south_paddle.south_paddlePos.x)) {
Debug.Log("hit the left zone");
}
}
}
This if statement will only run once per collision, as the :
OnCollisionEnter2D(Collision2D col) {
will only trigger when the object first collides. Any continued collision will not trigger a collision event. You would need to use :
OnCollisionStay2D(Collision2D col) {
For the event to be continuous and the if statement to activate repeatedly. Still a good idea to use brackets like in my example above, this way you can be certain how the statement is being executed. Hope this sorts out your problem.
Your answer
Follow this Question
Related Questions
Can't access nested if... 2 Answers
Odd collision behaviour with If/while statement 1 Answer
2D Pong Ball Not Reflecting Off Walls "Exactly" 1 Answer
Pong game collision problems 1 Answer