Search Unity

Breakout/Pong ball stuck to wall problem

Discussion in 'Scripting' started by g0tNoodles, Jun 6, 2012.

  1. g0tNoodles

    g0tNoodles

    Joined:
    Nov 28, 2010
    Posts:
    193
    Hi people, in my current project I have a ball, paddle and 3 walls, left, top and right. I have a problem where that if I get it just right (and I pretty much can whenever I want) I can make the ball slide up and down the right/left wall. Now, this isn't really the desired effect that I was looking for in a game where the ball is meant to constantly bounce around ^_^

    I have tried a couple of things; firstly, I tried adding extra velocity to the ball when it entered the walls collider/trigger. This worked in most situations apart from when the ball seems to hit the wall at a vertical straight line. Its almost as if the ball is somewhat stuck to the wall by static or a magnet. I then tried putting a couple of spheres in each corner to see how that would make the ball react as it was sliding up and down the wall. This gives some very random results!

    What I am hoping for is a solution or any help, in code or with something I can add to my ball/walls in order to stop this sliding motion.

    This is a video of what I mean:


    Cas
     
    Last edited: Jun 6, 2012
  2. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,659
    It looks like this is a problem you could run into at any point on the playfield, not just the sides - you can get the ball bouncing perfectly up/down in the center of the screen if you put the paddle in just the right place, no?

    How are you calculating the ball's bounce vector when it hits the paddle?
     
  3. g0tNoodles

    g0tNoodles

    Joined:
    Nov 28, 2010
    Posts:
    193
    Yes, you can make it bounce up and down from the paddle if you get it right. That's not too much of a problem as there will be objects that fall which you have to dodge; thus making you move the paddle.

    The code for making the ball a little more random off the paddle is this:


    Code (csharp):
    1. void OnCollisionExit (Collision collisionInfo)
    2.     {
    3.         Rigidbody rigid = collisionInfo.rigidbody;
    4.         float xDistance = rigid.position.x - transform.position.x;
    5.         rigid.velocity = new Vector3(rigid.velocity.x + xDistance - 0.2f, rigid.velocity.y, rigid.velocity.z + 0.2f);
    6.     }
    Excuse the numbers and the way it looks, I was messing about with it and haven't bothered to change it back yet. Depending on where the ball hits the paddle, the direction of the ball will head towards that direction. I could just ramp up the numbers to properly stop what you said but I have found that if I go above 32 velocity, the ball goes through the paddle.
     
  4. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,659
    You could simply add a small random value to the x component there:

    Code (csharp):
    1.  
    2. rigid.velocity = new Vector3(rigid.velocity.x + xDistance - 0.2f + Random.Range(-0.1f, 0.1f), ...
    3.  
     
  5. g0tNoodles

    g0tNoodles

    Joined:
    Nov 28, 2010
    Posts:
    193
    I see where you're coming from but unless I add larger number within the random range (like -2,2 or higher) it will not really affect the ball at all. So yeah, I can get it to come off the wall with higher numbers but then I get odd bounces off the paddle. For instance, I just did it with -1,1 and when the ball hit the right side of the paddle, the ball tried to go left.
     
  6. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,659
    OK, so ensure that the random value has the same sign as the position delta:

    Code (csharp):
    1.  
    2. rigid.velocity = new Vector3(rigid.velocity.x + xDistance + Mathf.Sign(xDistance) * Random.Range(0.1f, 0.2f), ...
    3.  
     
  7. g0tNoodles

    g0tNoodles

    Joined:
    Nov 28, 2010
    Posts:
    193
    That seems to work really well, thanks very much! =D
     
  8. JaredRollo

    JaredRollo

    Joined:
    Sep 11, 2012
    Posts:
    1
    Where in the code do i pust it for instance where would i place it in here exaclty?

    using UnityEngine;
    using System.Collections;

    public class BallMovement : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }
    }
     
  9. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    If you're using velocity, why not just make rigidbody.velocity.x = rigidbody.velocity.x * -1; when you hit it?