Moving a game object changing its direction on collision

Hi this is my first post.

I’m from an art background and have little to no knowledge of javascript.

I’m wanting to create a basic breakout clone and have a basic idea of what I want the code to do. I want the code to move the game object record it’s vector then on collision change it’s vector based on it’s current vector and point of collision (the ball bouncing off the walls etc in breakout).

I’ve had a search through the forum and the scripting reference but can’t work out what code I need to write. I’m pretty sure that I need to set the ball off at a speed on a vector which I can do but when it hits the wall it just bounces back and heads for the same spot on the wall so i figure that I need to change its vector on collision but I don’t know how to go about that.

If someone could point me in the right direction it would be most appreciated.

Cheers

Ben

Just give the ball a rigidbody component, set your physics material to bounce and let the physics engine handle the rest.

Use a slightly rounded collision mesh for your paddle, then the physics engine will even take care of varying angles according to where it strikes the paddle.

All in all there’s very little coding to be done to get this basic setup working. With the physics engine it all works auto-magically. You would only have to write a small couple lines of code for the collision callback on the bricks, to remove them as the ball strikes them.

Thanks Quietus,

I tried doing that in the first place but all I get is the ball hitting the wall bouncing back slightly then hitting the wall and repeating that.

the code I used to get the ball moving is:

var speed = 20;

function Update() {
transform.Translate(speed * Time.deltaTime, 0, 0);
}

I’ve also tried using gravity to get the ball moving but that moves a little too slow for what I’m after.

Cheers again

Get rid of your update movement, let the physics engine handle it. Use Rigidbody.AddForce to start your ball moving. You’ll also likely want to create a new physics material with 0 friction, apply that to your walls and your bricks.

Ok I’ve done that and it seems to be working!

Thanks alot you really helped me out and I learnt a bit more about Unity

Cheers Ben