- Home /
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;
}
}
}
}
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.
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
Follow this Question
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