Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Collision and rebound

Discussion in 'Scripting' started by alexikari, Sep 29, 2015.

  1. alexikari

    alexikari

    Joined:
    Aug 31, 2015
    Posts:
    21
    Hi everyone!

    I am creating a similar pong game (yes, another pong) and I am unable to make the rebound. The main problem is to calculate the angle of the rebound and to make the ball move. Currently, the ball move, but to calculate the angle continuously is problem.

    Ps: how post the code in better way?

    This is my code until this moment:

    // detect the ball collision
    void OnCollisionEnter(Collision col)
    {
    float contactPoint = 0;

    //if colide with the pads, execute action
    if (col.gameObject.name == "Cube1" || col.gameObject.name == "Cube2")
    {
    //return the contact point with paddle.
    foreach (ContactPoint contact in col.contacts)
    {
    contactPoint = contact.point.x;
    }

    BallSpeed(contactPoint);
    BallPosition();

    //if colide with the walls, execute action
    }
    if (col.gameObject.name == "rightWall" || col.gameObject.name == "leftWall")
    {
    BallPosition();
    }

    void Update () {
    // transform.Translate(eixoX, 0, eixoZ);
    //transform.Translate (new Vector3(radius * Mathf.Cos(angle), radius * Mathf.Sin(angle), 0));
    transform.Translate(Vector2.left * speed * Time.deltaTime);
    transform.eulerAngles = new Vector2(0, angleY);
    }
     
  2. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
  3. Dreamteck

    Dreamteck

    Joined:
    Feb 12, 2015
    Posts:
    336