Reverse an object's velocity? (Solved)

So I’m pretty new to the Unity Engine (read: about a week), but have some experience with game engines in general, and have years of experience with 3d apps like Maya.

I’ve been playing around with a bunch of random tests, then decided it was time to make a super-simple game with a goal and a win condition to prepare me for larger projects. My friend suggested Pong.

Everything about Pong seems pretty straightforward. The only problem I’m having is, how do I make the ball bounce off and continue in the opposite direction when it collides with another object (without losing speed)? I used a rigid body and gave it an initial force, but rather than bouncing upon collision, it just stops. I tried setting the drag and angular drag to zero and raising the force, but that didn’t work.

Any help would be greatly appreciated.

Use a physics material with a bounce factor of 1, and also probably reduce the bounce threshold in the physics manager.

–Eric

I thought it might involve physics materials. Only problem is, I’ve never worked with them.

How do I apply a physics material?

EDIT: Nvmd, I got it. Thanks for the help!

EDIT: Well, now, it bounces, but then immediately moves back to contact with what it bounced off of as if pulled in by gravity, bouncing smaller and smaller. It doesn’t bounce “off”, it just bounces.

EDIT: Turns out the problem was that I put “AddRelativeForce” in a Fixed Update, so it was constantly adding that force. Now it works, but the ball still slows down over time. I want the speed to remain constant, or to even increase over time. Why would collisions slow it down on full bounciness with zero drag?

What setting are you using for the Bounce Combine on the physic material? If you use Maximum, the ball’s bounciness value of 1 should override any other value.

Yeah, I already figured it out. But what I did was apply the material to all the colliding objects so everything had a value of 1. Your way is simpler though.

Anyway, I’m having trouble destroying and respawning the ball. When it collides with the wall, BOTH it and the wall are destroyed and respawned in the middle of the play area. I guess part of the problem is I’m using code I’ve looked up in the documentation and don’t yet fully understand.

Here’s the script. I’m adding one version to the left wall, and one version to the right wall, so each player has their own goal:

var Player2Score = 0;
function OnTriggerEnter (other : Collider) {
Destroy(other.gameObject);
Player2Score += 1;
Instantiate(other.gameObject, Vector3(0, 0, 0), transform.rotation);
}

When the collision happens, BOTH the ball and the wall are respawned. I want to leave the wall alone. and I don’t really understand what “(other: Collider)” is for, why’s there a colon? It works correctly if I place the script on the BALL and take away all the “other” stuff, but then I can’t account for which goal its hitting and set the score.

Preferably I’d want to destroy and instantiate the ball by name, but I couldn’t figure out how to do that.

Also, I don’t know what command to use to set the rotation to a desired value.

EDIT: It turns out the spawning problem was because I’d left the collision script on the ball when I added it to the walls. My other questions still stand, though.

Is this for the ball physics or setting up the wall blocks or… ?