I’m currently trying to make a Pong game and I need to give the ball a constant velocity, I’m going to make it so it starts moving left in the X Axis so that it hits the players paddle first and then I hope that Unity’s physics engine will do the rest. Although I will have to adapt it so it bounces off the paddle instead of just grinding to a hault etc. However I can’t seem to give the ball a constant velocity for some reason. Here is the code:
#pragma strict
var v = new Vector3(0.0f, 0.0f, 0.0f);
var speed = 10.0f;
function Start () {
GetComponent.<Rigidbody>().velocity = v;
var movement = new Vector3(-1.0f, 0.0f, 0.0f);
v = speed * movement;
}
function Update()
{
}
function OnCollisionEnter(c : Collision) {
if (c.collider.name == "Player" || c.collider.name == "Opponent")
v.x = -v.x;
else if (c.collider.name == "Wall")
v.z = -v.z;
}