Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 m0mohy92 · Jun 21, 2019 at 04:18 AM · collision2d game2d sprites

Why my ball acts if there is a hidden collider?

I am making a ping pong game, it works well, but when the ball hit the border and assumed to disappear and respawned again, after hitting it it doesn't reach the other paddle , but it reflect at fixed point as if there is a collider (See below) and when hitting it again it acts properly, another problem : when the ball hit the borders, a counter is incremented and a message saying if the player won or the AI won, but every time the player counter is incremented the other counter is incremented too, and the only printed message when the AI player win. Here is the code : using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System.Text; using System.IO;

 public class PingPongBall : MonoBehaviour {
 
     private float forcex=6f, forcey=15f;
     private Rigidbody2D mybody;
     private int c =0;
     private int c1=0;
     public Text Counter;
     public Text Counter1;
     private Renderer rend;
     public GameObject Ppaddle;
     public GameObject Apaddle;
     int i=0;
 
     void Start() {
         mybody=GetComponent<Rigidbody2D> ();
         Ppaddle = GameObject.Find ("Player_Paddle");
         Apaddle= GameObject.Find ("AI_Paddle");
         Counter.text ="Player1 :" + c.ToString ();
         Counter1.text ="Player2 :" + c1.ToString ();
         rend =this.GetComponent<Renderer>();
         rend.enabled = true;
     }
 
 
 
     IEnumerator pause ()
     {
         mybody.velocity = new Vector2 (0f, 0f);
         yield return new WaitForSeconds (1.5f);
         rend.enabled = true;
         mybody.velocity = new Vector2 (10f  , -14f );
 
     }
 
 
 
     void WinnerCheck() {
         if (i==5){
         if (c1 > c) {
             print ("You win!");
         }
 
         if (c > c1) {
             print ("You loose!");
 
         }
     }
     }
 
 
     void OnCollisionEnter2D (Collision2D col)
     {
         if (col.gameObject.name == "Player_Paddle") {
             
             
             if (Input.acceleration.x > 0f) {
                 mybody.velocity = new Vector2 (10f, 14.5f);
 
             } else if (Input.acceleration.x < 0f) {
                 mybody.velocity = new Vector2 (-10f, 14.5f);
 
             } else {
                 mybody.velocity = new Vector2 (0, -14.5f);
 
             }
         }
 
         if (col.gameObject.tag == "AI_Paddle") {
             mybody.velocity = new Vector2 (forcex, -forcey);
         }
 
         if (col.gameObject.tag == "Top") {
             i++;
             if (c1 <= 4) {
                 c1++;
                 Counter1.text = "Player2 :" + c1.ToString ();
                 rend.enabled = false;
                 transform.position = new Vector3 (3.5f, 1f, 0);
                 StartCoroutine (pause ());
             } else {
                 WinnerCheck ();
                 gameObject.SetActive (false);
                 Time.timeScale = 0;
             }
         }
 
 
         if (col.gameObject.tag == "Ground") {
             i++;
             if (c <= 5) {
                 c++;
                 Counter.text = "Player1 :" + c.ToString ();
                 rend.enabled = false;
                 transform.position= new Vector3 (3.5f, 1f, 0);
                 StartCoroutine(pause());
 
             } else { 
                 
                 WinnerCheck ();
                 gameObject.SetActive (false);
 
                 Time.timeScale=0;
             }
     }
 }
 }
 
 


Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DanVioletSagmiller · Jun 21, 2019 at 05:43 AM

I haven't found the source of your problem, but I'm seeing a variety of code smells, that resolving will often clearly show the source of the problem. (part 4 might be the answer, but your question is not clear.)

1) Variable are not clearly named. C and C1 appear to be important, yet the name tells me nothing about what they are for. Please rename the variables with names that express its purpose. Its better to have a longer name, than a name that won't make sense until you look through all the code to see what it does. As C/C1 disguise the purpose, this will make it a likely bug hide out, as it may get miss used, even in a matter of minutes.

2) int i is held at a the class scope, but its intention is for immediate purpose

3) gameobject name or tag for comparisons. Tags are arguable, but I still tend to disagree with their use. validating that an object is correct based on the name tends to be a big no in Unity. Basically, any tie between your C# code and the inspector should be clearly expressed. For instance, any type of object that you want to interact with on contact could carry a specific monobehaviour. You should see if the object has that type. This establishes a a very clear code comparison. If you change the name of the game object, your code will still work. Or, in the case of having 2 players, and 1 ball, it could literally aready reference the exact object as a field. "public PlayerComponent PlayerReference" to wire them up.

4) Magic numbers Line 74 and line 90 compare C2 to either a 4 or a 5, without much clear reason why. It would be better than you name. It appears 5 is required to call a complete game, but you stop C2 from incrementing when the ball hits the ceiling. only until it reaches 4. Perhaps that is the issue? I strongly recommend avoiding magic numbers. Put a const, or a public field settable through the inspector, as a variable, instead of just a number. If the 4 and 5 where supposed to be the same number, having used a variable instead of a magic number would have resolved this for you.

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 DanVioletSagmiller · Jun 21, 2019 at 05:58 AM 0
Share

Sorry, I got lost a little ways in, and started calling the use of C/C1 C2. But I believe that helps backup #1, that by using poorly named variables, it makes it easy to mix up or forget their original purpose.

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

187 People are following this question.

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

Related Questions

One or many scenes? 4 Answers

Problems with 2D Collision 3 Answers

Best approach for 2d animation to interact with the environment? 0 Answers

create game 2d linking stars 0 Answers

Unity 2D TileMap Rule Tile Carpet 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