Help about ping pong game

I made ping pong game to Unity 5.

using UnityEngine;
using System.Collections;
public class Ball : MonoBehaviour {
public float ballVelocity = 1000;
Rigidbody rb;
bool isPlay;
int RandInt;
void Awake ()
{
rb = gameObject.GetComponents ();
RandInt = Random.Range (1, 3);
}
void Update ()
{
if (Input.GetMouseButton(0) == true && isPlay == false)
{
transform.parent = null;
isPlay = true;
rb.isKinematic = false;
if(RandInt == 1)
{
rb.AddForce(new Vector3(ballVelocity,ballVelocity,0));
}
if(RandInt == 2)
{
rb.AddForce(new Vector3(-ballVelocity,-ballVelocity,0));
}
}
}
}
There is code. I don’t know what there is wrong can somebody help me.

You don’t really describe a problem here, though I can tell you that this is a little heavy handed to make a ball bounce back and forth. Have you taken a look at any of the 2D examples?

I think that’s really where you need to start :slight_smile:

Otherwise, if you can describe what your problem is here, I can try to help. I’ll give a whack at what you SHOULD be seeing based on what I quickly read in the sample above (Also, use the "Insert Code feature here… makes it more readable…)… Seems like you are just trying to take a random number, and based on that number reverse the exact trajectory of the rigidbody. However, you are doing this on both axis equally… So, assuming all things are normal, depending on how the numbers are generated, you could get a ball that just sits there, or it would fly all over the palce (like say the number generator grabbed, 1, 2, 1, 2, 1, 1, 1, 1… It would fly to the right, then back to the start (exact same path if you are in FIXEDUpdate, in your case you could get some weird behavior when you get any lag… Try to use FixedUpdate for physics stuff when you can…), then back to the right, repeating till it read two 1’s back to back, then (beacuse you have never reset the velocity, its going to add even MORE velocity to that bad boy and launch it into orbit :wink:

Try the tutorials before the forums, you will find the samples VERY comprehensive and easy to follow. You will be a ping pong champ in no time :wink:

Adam