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

Bouncing Ball

Discussion in 'Scripting' started by svprdga, Aug 24, 2012.

  1. svprdga

    svprdga

    Joined:
    Aug 21, 2012
    Posts:
    14
    I've passed many hours reading some posts in this forum about making a bouncing ball that does not loose energy in the hits. I am trying to do that in a 3D classic Pong game I'm making; some people say to create a bounce material with bounciness to 1 and no friction; then apply to all objects; but that isn't working in my project. Some other people talk about the Vector3.reflect() method, but also this isn't working for me.

    The script applied to the ball:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class BallPhysicScript : MonoBehaviour {
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.         this.rigidbody.rigidbody.AddForce(Vector3.right * 1000);
    10.     }
    11.    
    12.     private void OnCollisionEnter(Collision collision){
    13.         //What should I put here?
    14.     }
    15. }
    16.  
    Thanks for any help.
     
  2. TwiiK

    TwiiK

    Joined:
    Oct 23, 2007
    Posts:
    1,729
    Perhaps something like this?
    Code (csharp):
    1. void OnCollisionEnter(Collision collision) {
    2.     // This is where the ball hit the object it collided with
    3.     ContactPoint contact = collision.contacts[0];
    4.  
    5.     // This is the normal (angle?) of the surface that was hit
    6.     Quaternion newDirection = contact.normal;
    7.  
    8.     // The speed your ball had at the point of impact
    9.     float speed = rigidbody.velocity.magnitude;
    10.  
    11.     // Set a new, reflected, velocity for the ball
    12.     // making it go in the opposite direction of the surface that was hit
    13.     rigidbody.velocity = newDirection * speed;
    14. }
    No idea what result this produces as I'm currently at work and not really that skilled with vectors in Unity. :p

    There's one thing that could pose a problem and that is if the velocity of the ball has already been affected by the collision when we're inside OnCollisionEnter(). If that is the case then you would have to get the velocity from somewhere else. Perhaps store it when you instantiate the ball or if it changes throughout store it in Update() or something.

    This also doesn't take into account the angle your ball hit the surface at when calculating the new velocity so it won't look realistic, but perhaps its a start. :)
     
  3. svprdga

    svprdga

    Joined:
    Aug 21, 2012
    Posts:
    14
    Deffinetively not working; the ball gets hung to the limits sometimes, and the reflections are not real.
     
  4. Loius

    Loius

    Joined:
    Aug 16, 2012
    Posts:
    546
    For something like Pong it's best to simulate physics yourself - give the ball a direction based on where it hits the paddle, never let its speed decrease, always bounce it directly off of walls. You're not getting the results you want because there's no such thing as predictable physics (well... not -completely- predictable). Using physics would be more useful in something where the user can't tell that it's off by a bit - baseball, ragdolls, collapsing structures, that sort of thing.

    You can still use a nonkinematic ball, just give it an OnCollisionEnter method and directly set its velocity as needed.

    Code (csharp):
    1. function OnCollisionEnter( info : CollisionInfo ) {
    2.   if ( info.GetComponent<Paddle>() ) {
    3.     rigidbody.velocity = (transform.position - info.transform.position) * currentBounceSpeed;
    4.   } else {
    5.     // deflect off the surface - it's possible that builtin physics with full bounciness can do this step all on its own, since the user shouldn't be able to see any tiny inconsistencies here
    6.     // if that doesn't work, .Reflect is the function to use
    7.   }
    8. }