Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by flwns · Oct 13, 2014 at 08:48 PM · collisionifpongnested

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.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image 767_2 · Oct 13, 2014 at 08:56 PM 0
Share

just for no reason change print to debug.log

avatar image JoeStrout · Oct 13, 2014 at 08:57 PM 0
Share

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?

avatar image jctz · Oct 13, 2014 at 09:43 PM 0
Share

Or at leasts include the values of ballPos.x and south_paddle.south_paddlePos.x in your first Debug.Log() call. :)

avatar image flwns · Oct 14, 2014 at 02:08 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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");
         }
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MrSoad · Oct 13, 2014 at 11:36 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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

On collision enter not being activated 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges