I need a ball to bounce around inside 4 walls and not lose it’s speed. Pong without paddles if you will.
I’ve searched through the forums a lot, but none of the solutions seem to have addressed this, only 1 axis bouncing is around.
How could I achieve this? Thanks!
I’m doing this right now, yeah it can be improved, but I just wanted it to work for now.
#pragma strict
var speed : float;
function Start () {
rigidbody.velocity = Vector3 (speed,0,speed);
}
function Update () {
}
function OnCollisionEnter(collision : Collision) {
if (collision.transform.name == "topwall") {
if (rigidbody.velocity.x < 0 ) {
rigidbody.velocity = Vector3 (-1 * speed,0,-1 * speed);
} else {
rigidbody.velocity = Vector3 (speed,0,-1 * speed);
}
}
if (collision.transform.name == "bottomwall") {
if (rigidbody.velocity.x < 0 ) {
rigidbody.velocity = Vector3 (-1 * speed,0,speed);
} else {
rigidbody.velocity = Vector3 (speed,0,speed);
}
}
if (collision.transform.name == "leftpaddle") {
if (rigidbody.velocity.z < 0 ) {
rigidbody.velocity = Vector3 (speed,0,-1 * speed);
} else {
rigidbody.velocity = Vector3 (speed,0,speed);
}
}
if (collision.transform.name == "rightpaddle") {
if (rigidbody.velocity.z < 0 ) {
rigidbody.velocity = Vector3 (-1 * speed,0,-1 * speed);
} else {
rigidbody.velocity = Vector3 (-1 * speed,0,speed);
}
}
print (rigidbody.velocity);
}
Gonna take a break now. Long Unity learning day.