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. Dismiss Notice

Pong Ricochet Help please

Discussion in 'Scripting' started by TaleOf4Gamers, Jan 9, 2015.

  1. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    Hey,
    I was wondering if someone could help me with my pong game.
    I want to get the ball to ricochet depending on where it hits the racket.
    I have searched the internet but I could not find any good scripts.
    Thanks.
     
  2. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
  3. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    Thats what I want, I will try to code something but im still in the process of learning so i may need some help. ;)
     
  4. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
  5. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    Can anyone help me?
     
  6. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    Is it really that hard, Vector3.Reflect, on collision with the paddle you just get the last know direction the ball was moving in, the surface normal of the face that got collided with. Plug that into Vector3.Reflect and it will return you the direction the ball should be going in after the bounce.
     
  7. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    It is for me.
    Sorry if I seem dumb but i'm still learning (Thats what everyone says, I know)
     
  8. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    http://docs.unity3d.com/ScriptReference/ContactPoint-normal.html get the normal of the collision
    and assuming your ball or whatever has/is using a rigidbody:

    Code (CSharp):
    1. void OnCollisionEnter(Collision other)
    2.     {
    3.         //also probably put some sort of tag check or something to make sure we hit the paddle or whatever
    4.         rigidbody.velocity = Vector3.Reflect(rigidbody.velocity, other.contacts[0].normal);      
    5.     }