Bouncing Ball

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:

using UnityEngine;
using System.Collections;

public class BallPhysicScript : MonoBehaviour {

	// Use this for initialization
	void Start () {
		this.rigidbody.rigidbody.AddForce(Vector3.right * 1000);
	}
	
	private void OnCollisionEnter(Collision collision){
		//What should I put here?
	}
}

Thanks for any help.

Perhaps something like this?

void OnCollisionEnter(Collision collision) {
    // This is where the ball hit the object it collided with
    ContactPoint contact = collision.contacts[0];

    // This is the normal (angle?) of the surface that was hit
    Quaternion newDirection = contact.normal;

    // The speed your ball had at the point of impact
    float speed = rigidbody.velocity.magnitude;

    // Set a new, reflected, velocity for the ball
    // making it go in the opposite direction of the surface that was hit
    rigidbody.velocity = newDirection * speed;
}

No idea what result this produces as I’m currently at work and not really that skilled with vectors in Unity. :stuck_out_tongue:

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. :slight_smile:

Deffinetively not working; the ball gets hung to the limits sometimes, and the reflections are not real.

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.

function OnCollisionEnter( info : CollisionInfo ) {
  if ( info.GetComponent<Paddle>() ) {
    rigidbody.velocity = (transform.position - info.transform.position) * currentBounceSpeed;
  } else {
    // 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
    // if that doesn't work, .Reflect is the function to use
  }
}