Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Problem getting contact points when another collider hits

Discussion in 'Getting Started' started by xIncrement, Feb 19, 2019.

  1. xIncrement

    xIncrement

    Joined:
    Feb 6, 2019
    Posts:
    2
    Hi,
    I'm creating a breakout game, and I've set up the basics of the game.
    You can launch the ball, it'll bounce and break the bricks it touches, if it touches the ground under the paddle you lose a life - If you lose all your lifes you get a game over, and if you clear all the bricks with your life intact, you win!

    Now, I want to implement the classic of breakout games, where you influence the ball' direction after hitting the paddle gradiently based on where on the paddle the ball lands.
    For example if the ball comes in from the left but hits the very left end of the paddle, the ball should bounce back. Vice versa for right.

    What i'm trying to do, is to use the paddle' box collider' contact points, so I can record WHERE on the paddle the ball lands, so I can do a mathematical formular to calculate which X direction the ball should go.

    This is my code:
    void OnCollisionEnter(Collision collision)
    {
    Collider myCollider = collision.contacts[0].thisCollider;

    Vector3 victor = myCollider.ClosestPoint(ball.transform.position);

    Debug.Log("Contact point: ");
    Debug.Log(victor);
    }

    When i use that piece of code I can only get the contact points of where the ball hits in the scene.
    In other words, I can get data on where the ball hits IN SPACE but NOT on the paddle.

    I've checked the forums, Google and Youtube the last 2 days. It's so frustrating.

    What am I doing wrong? Can you even do it like I want to do it?


    unityscreencap.PNG
     
  2. xIncrement

    xIncrement

    Joined:
    Feb 6, 2019
    Posts:
    2
    I figured it out!!

    Since you only wanna figure out where the ball is, as it's hitting the paddle, you can take the position of the paddle and compare it to the ball.
    Now the position would be the middle of the paddle's x-position, so you want to take the distance from the center of the paddle to where the ball lands on the paddle, then use that to enhance your force either slightly or massively.

    To keep the same force of the ball, so it doesn't speed up, I added this in the 'void Update' method:
    rb.velocity = rb.velocity.normalized * 14f;

    I did this code in the 'void OnCollisionEnter' method for when it hits the left side of the paddle:
    // If the ball hit the left side of the paddle
    if (ball.transform.position.x < gameObject.transform.position.x)
    {
    Debug.Log(gameObject.transform.position.x - ball.transform.position.x);

    float x = (gameObject.transform.position.x - ball.transform.position.x) * -500f;

    rb.AddForce(new Vector3(x, 0, 0));
    }


    Then added an if-statement for the right side of the paddle.
    So now, It'll add a force on the x-axis, depending on where you hit the ball!

    Hope it'll be useful for someone.