Search Unity

2D bouncing balls getting stuck in a wall

Discussion in 'Physics' started by KazamiSan, Dec 14, 2018.

  1. KazamiSan

    KazamiSan

    Joined:
    Dec 14, 2018
    Posts:
    4
    I've been trying to make a basic game where whenever the player clicks on the screen, it generates a circle/ball at the mouse position. All the balls should be bouncing endlessly inside the screen, which works just fine, except for some cases where a few balls get stuck in walls like so:

    Very weird behavior, as I've followed what a tutorial has already said about bouncing balls (although it was a Pong tutorial)
    The circles a 2D Circle Collision and a 2D Rigidbody attached to them, as well as a custom script that goes like the following:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class I : MonoBehaviour
    6. {
    7.  
    8.     public float speed = 1;
    9.  
    10.     public TextMesh text;
    11.  
    12.     Vector2 top, bottom, direction;
    13.  
    14.     // Use this for initialization
    15.     void Start() {
    16.         direction = Random.insideUnitCircle.normalized;
    17.         top = Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, Screen.height));
    18.         bottom = Camera.main.ScreenToWorldPoint(new Vector2(0, 0));
    19.     }
    20.  
    21.     void Update() {
    22.         float size = transform.localScale.x / 2;
    23.         float x = transform.position.x;
    24.         float y = transform.position.y;
    25.         if (x < bottom.x + size)
    26.             direction.x = -direction.x;
    27.         else if (x > top.x - size)
    28.             direction.x = -direction.x;
    29.         else if (y < bottom.y + size)
    30.             direction.y = -direction.y;
    31.         else if (y > top.y - size)
    32.             direction.y = -direction.y;
    33.         Vector2 finalDirection = direction * Time.deltaTime * speed;
    34.         transform.Translate(finalDirection);
    35.         text.text = finalDirection.ToString();
    36.     }
    37. }
    38.  
    And here's my code that is attached to my main camera:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class spawnCircle : MonoBehaviour
    6. {
    7.  
    8.     public GameObject I;
    9.  
    10.     // Use this for initialization
    11.     void Start() {
    12.  
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update() {
    17.         if (Input.GetMouseButtonDown(0)) {
    18.             Instantiate(I, Camera.main.ScreenPointToRay(Input.mousePosition).origin, new Quaternion(0,0,0,0));
    19.         }
    20.     }
    21. }
    22.  
    Is there an error within either of my scripts that I can modify to solve this problem? And if there are better ways to do what I'm trying to do here that might eliminate the errors, please let me know. Thanks in advance!
     
  2. KazamiSan

    KazamiSan

    Joined:
    Dec 14, 2018
    Posts:
    4
  3. N_Murray

    N_Murray

    Joined:
    Apr 1, 2015
    Posts:
    98
    It might be because you're using translate to move your object instead of using the rigidbody and physics. If you move using the transform it can ignore collisions. You should look into rigidbody.AddForce