Search Unity

Arkanoid in Unity | Ball Movement

Discussion in 'Scripting' started by paco, Jul 23, 2011.

  1. paco

    paco

    Guest

    Joined:
    Oct 28, 2010
    Posts:
    20
    hi..

    i'm trying to code Arkanoid in Unity, and i'm not really sure how to do the ball movement thing the best way.
    searching on the web, i found this post (its a post about the Pong ball movement):

    http://answers.unity3d.com/questions/28037/question-pongball-to-change-direction.html

    i prety much tryed to do everything like advised in the post,
    but it's not working (ball never actually behaves the right way)

    the code i'm using for the ball is this one:

    PHP:
    var velocity Vector3;

    function 
    Update() {
        
    transform.Translate(velocity Time.deltaTimeSpace.World);
    }

    function 
    OnTriggerEnter(other Collider) {
        
    //All collision objects must be oriented to face the play area
        
    velocity Vector3.Reflect(-velocityother.transform.forward);
    }
    i tried changing other.transform.forward to transform.up and everything else but nothing worked.

    so my question is, how i would implement the ball movement the best way..
    if unity physics can do everything for me, i would like to do that. :)
    if a raycast is the better solution, then i would try do that... :D

    please advice
    regards
     
  2. oneXwork

    oneXwork

    Joined:
    Mar 3, 2011
    Posts:
    266
    maybe you can use Vector3.Distance
    to calculate the dist is like it <=0
    then call the reflect function
     
  3. phreak

    phreak

    Joined:
    Jan 24, 2011
    Posts:
    34
    this is what i did:
    set Drag and Angular Drag of your ball to 0.

    created bouncy physics material (u have similar in unity asets):
    Dynamic Friction :0
    Static Friction: 0
    Bounciness: 1
    Friction Combine: Minimum
    Bounce Combine: Maximum
    -- and everything else to 0!

    Than on Project settings -> Physics set Bounce Threshold to 0.
    If u don't do that and your ball movement speed is slow it will stay stuck in walls.

    now just set velocity in desired direction and ball will bounce perfectly from all colliders. No need for calling update method to handle your physics.

    One more thing I did was to make sure that ball position and velocity in z direction is 0 in OnCollisionExit method.

    And one more tip. If you are using velocity you don't need to use Time.deltaTime. I just learned that recently :)
     
    npetrovic04 likes this.
  4. brzozowsky

    brzozowsky

    Joined:
    Sep 12, 2011
    Posts:
    39
    Hi, thanks for that!
     
  5. Vitalii

    Vitalii

    Joined:
    Mar 2, 2012
    Posts:
    9
    2 phreak: wow, your information is priceless.. i spent all day long, trying to figure out, why ball bouncing wrong sometimes.. but now looking like it's prefect.