Question : Pong(ball to change direction)

hello I am creating this pong game to train on but I am faced with a problem that I can't solve. I read everywhere but no one really helped a lot.They told me to find the distance but then what?? So here is my problem :

How can i make my ball change direction depending on the place it hits the cube. For example if the ball hits it from down it changes direction accordingly.Here is my code till now

    var other : Transform;
var dist :int;
function Update () 
{

if (other) {
dist = Vector3.Distance(other.position, transform.position);
print ("Distance to other: " + dist);
}

}

function OnTriggerEnter(collisionInfo : Collider)
{
if(collisionInfo.gameObject.tag == "player1")
{
//transform.position.x = 1130.362;
rigidbody.velocity = Vector3.left * 4000;

}

if(collisionInfo.gameObject.tag == "player2")
{
//transform.position.x = -1130.362;
rigidbody.velocity = Vector3.right * 4000;

}

}

thank you

I'm not really sure why your script is trying to do things the hard way, but I'll try to give you the simplest advice that I can to making a "pong" game. You say people told you to find a distance, but I'm not sure why you would need that - what sort of questions did you ask?

Physics = Pong

Unity's physics system can do most of the work for you as you may have noticed since you're using a rigidbody. If the paddles and the walls have colliders and your ball has a rigidbody, then your Pong game is almost done. Create a physic material with bounciness set to 1 and no friction and apply it to everything. Write a script to position and start your ball moving with AddForce or by setting velocity once when the player in possession of the ball fires it (and when you first start if a player doesn't start in possession of the ball). Add triggers to the ends of your play area with scripts to manage scoring and who has control of the ball when someone scores). You have Pong.

Pong la mode

Not using physics to do the work, you would actually have to think and do some setup, but Pong's rather easy. Assuming everything is the same as above, except you have no rigidbody or Physic material and you are moving the ball yourself, all you would need different is the ball's movement/trigger script and to make some assumptions about your setup.

var velocity : Vector3;

function Update() {
    transform.Translate(velocity * Time.deltaTime, Space.World);
}

function OnTriggerEnter(other : Collider) {
    //All collision objects must be oriented to face the play area
    velocity = Vector3.Reflect(-velocity, other.transform.forward);
}

That's kind of boring though, as the speed of the paddles at the time of impact isn't really taken into account, so let's add that.

var velocity : Vector3;

function Update() {
    transform.Translate(velocity * Time.deltaTime, Space.World);
}

function OnTriggerEnter(other : Collider) {
    //All collision objects must be oriented to face the play area
    velocity = Vector3.Reflect(-velocity, other.transform.forward);

    //paddles are tagged player, but named differently
    if(other.gameObject.tag == "player") {
        //Paddles have a script called PlayerControl which contains their velocity
        var script : PlayerControl = other.GetComponent(PlayerControl);
        velocity += script.velocity;
    }
}

You can easily make the game using physics and rigid bodies so long as the ball doesn't move too fast the the walls aren't too thin.

You can set the movement to a single plane by constantly setting z velocity to 0 on the ball and set the rigid body to have 0 drag and no gravity and freeze rotation to be on the safe side.

Then you set all the bounce properties of every material to maximum bounce and 1 bounciness to make sure the ball doesn't slow down with each hit.

How do i put that in here like where would i put it in this script and is that all i have to do ?

using UnityEngine;
using System.Collections;

public class BallMovement : MonoBehaviour {

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}

}