Block Breaker Ball Movement?

Hello Guys,

I have been going through the lectures on this game haven’t finished yet, so, please if this movement is rectified in future lectures then tell me to crawl back under my rock and shut the hell up :grinning:.

Anyway, we set up the movement: in the script via:

  • this.rigidbody2D.velocity = new Vector2 (2f, -10f);

It’s good and gives a nice consistent movement for the ball, unless you hit it with the side of the paddle in which case it flies off every which way. Now in the original Arkanoid which I wistfully whiled away many an hour with the old Crummybore 64 - the movement as I recall was based on where if made contact with the “bat” as I will call it. I am going to change the graphic for the paddle to be longer and more like the “bat” in Arkanoid make it a little longer than the paddle.

Now, I would like to try and implement that type of movement, as it also makes it incredibly hard to put the game into a “boring” loop, move the bat a little and it changes the angle of movement. Now I don’t know enough about Unity and/or coding to go about it myself, so, I am hoping that some of you guys will be able to help me do this. And might need to include some more esoteric Unity stuff that I don’t know about. So, to do this, I’ll ask a series of questions to try and learn all the little steps that might make it possible. So I don’t want some person to write a complex series of coding with numerous scripts to make it work, I just want answer to the questions posed, and if it turns out that it’s a difficult task then I’ll deal with that when the time comes.

So, the game starts of and ball’s velocity is set in some random direction but usually with x direction being in the positive realm otherwise it just gets plain annoying. But after that the next contact determines the direction of the ball. If the contact is dead in the centre of the bat then ball moment is straight up providing you can get it dead centre, if contact is extreme right then it flies off to the right and same on the left and you have all those range of angles in between those two extremes.

So, the first question would be: is it possible to determine where the ball intersects the bat on the x and Y axis? And I suppose just as importantly can we do that through the OnCollisionEnter2d script? Or are we looking at something different here? And can we calculate the offset from the centre of the bat?

That’s all I can think of for now!

EDIT: much thanks in Advance :sunglasses:.

i did via oncollisionenter 2d - in a script i deduct x axis of the ball and x axis of the paddle and adjust the bounce via script by setting appropriate x component of the speed to the ball (and y as well btw).

i described it here THE WORST Arkanoid

(the script is kinda lame probably. i guess on trigger would be better - i suppose it it faster. and there are some other not to smart things in a script. but it works.)

I posted this a little while ago, it should give you what you need with regards to the point of impact;

Many thanks to both of you guys for responding, when I get some free time I will go over your code samples, print them out and whatnot and see what little gems I can pick up from the both of them. Some of the things you have done are a little more advanced than my current experience so it’s going to take some time to digest and rummaging around in the Unity docs. Well at least I might get some extra sleep from that :smiley:.

I will also post any further questions if I have any after that.


1 Like

Rob I have been going through you code for getting X,Y position in local co-ords and I seem to be able to follow this code and is very close to the example code given in Unity docs - which really helps, readability and the ability to check code in the future. But I just want a couple of things clarified.

I realize that this is going to come across as a dumb question, but, I am assuming we put this on the Paddle/Bat?

And more importantly, in your code you wrote:

  • Vector3 paddleLocalImpactPoint = _paddle.transform.InverseTransformPoint(impact.point);

And I noticed an underscore after the equals sign and before the “paddle” I would like to know the purpose of that - it couldn’t see it in Unity docs to do with the InverseTransformPoint and so need to know why it was used and/or if it is necessary.

Thankyou for your patience.


You could, but would need to change around some code and what happens next. The example I gave was actually taken from the Ball. As it is the Ball which I wanted to change the outbound vector for, not the paddle.

Within the OnCollision2DEnter() method I added some code to determine what was being collided with and handle it accordingly. In the case of the paddle I would then use the above code example.

It’s just a naming convention I use for fields within a class. For example;

using UnityEngine;

public class Ball : MonoBehaviour
    private Paddle _paddle = null;

    /// <summary>
    /// Initialisation
    /// </summary>
    private void Start()

    /// <summary>
    /// Initialises the Ball
    /// </summary>
    private void Initialise()
        _paddle = GameObject.FindObjectOfType<Paddle>();

Facepalm! D’oh what was I thinking. Of course it would be the ball! I think I need some sleep…

Thanks for the clarifications :blush:.

1 Like

hehe, don’t beat yourself up about it… you could do it either way… either on the ball and find the paddle or on the paddle and find the ball…

I tend to try to make a sentence of what I want to achieve on each feature (user stories), and that then helps determine which objects do what…

So, for example you might say;

“As a player, I would expect the ball’s direction to be determined on the point of impact on the paddle.”

From this I can take the following;

  • player
  • ball
  • paddle

In my sentence above I have written “…the ball’s direction…”, thus this tells me that I need to add functionality to that object, but also using “…point of impact on the paddle”, this tells me that there will be;

  • a collision between the two

and that I will require;

  • their positions at the point of impact

You could also write the sentence from the perspective of the paddle and have it “forcing” the ball away, or “hitting” it away, but then when you review both sentences you will need to make a call on which makes the most sense from the perspective of the game :slight_smile:

It’s a fairly simple technique and one that can be very useful :slight_smile:

1 Like

Wow! I’ve never thought of coding like that :open_mouth:. It makes so much more sense to me to do it this way.

I am going to start using that.

Are you an instructor here on Udemy?

Wow! I’ve never thought of coding like that :open_mouth:. It makes so much more sense to me to do it this way.

Have a search for User Stories and Use Cases (look specifically for references to verbs and nouns), should help with defining a GDD and helps to remove ambiguity when it comes down to designing your objects.

Are you an instructor here on Udemy?

Nope, just your friendly Forum Gremlin :slight_smile:

Privacy & Terms