About 'Distance As A Vector2'!


In this video (objectives)…

  1. Understand how to calculate the distance from ball to paddle.
  2. Update location of the ball to match the location of the paddle plus our offset.

After watching (learning outcomes)… Stick the ball to the paddle.

(Unique Video Reference: 10_BR_CUD)

We would love to know…

  • What you found good about this lecture?
  • What we could do better?

Remember that you can reply to this topic, or create a new topic. The easiest way to create a new topic is to follow the link in Resources. That way the topic will…

  • Be in the correct forum (for the course).
  • Be in the right sub-forum (for the section)
  • Have the correct lecture tag.

Enjoy your stay in our thriving community!



Hello Rick,

I did a little different. Instead of:

paddle.position = paddlePos + paddleToBallVector;

I used:

paddle.position = new Vector2(paddlePos.x, paddleToBallVector.y);

Any problem with this solution despite the fact that is longer?


[Another question]

I’m not using a ball and a paddle, but a baby and a troller.

I set up the baby to be behind the troller, but after this code to glue both together, the baby moved to ahead.

You will talk about this in the future?
Can you give me a hint about how to solve that?


Hi, I did the calculation as you have presented in video, and also in this way:

transform.position = (Vector2)paddle.transform.position + spacer

and it works - ball folows the paddle. However, after about 5 seconds of playing around with the ball, the collision event is triggered by the collider below the play area, caused by a collision with the CircleCollider2D which is attached to the ball. Why could that be? The ball is on the paddle.

ok so I have found out that if the rigidbody 2d on the ball is set to kinematic, problem does not occur. But then won’t things be broken when the ball launches ?

void Start () {
        spacer = transform.position - paddle.transform.position;
        GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Kinematic;

fixed the issue. Only when the ball launches, i have to reset this back to dynamic.



What fixed it for me was adding this to Update():

GetComponent<Rigidbody2D>().velocity = Vector2.zero;

I am not sure why, but my guess is that as time goes on, the ball gets faster and faster. In the code, we only change the position, never the speed. What might occur is that the speed never gets reset, but we can’t see it because we’re fiddling with the transform. As it gets faster and faster, it might end up reaching the collider beneath the level before we get to adjust the transform on that frame.

For example, the further down I put the collider, the longer it takes for it to trigger. That would also explain why switching to kinematic instead of dynamic would fix it.

Just a guess really.




yea. I crash in this problem too .I just get idea to listen what is going on so I put a Debug.Log on every update asking rigitbody about his velocity and …you are right.The speed its higher every second.but i also put a listener on the transform.position and the position is never change …so question is how this is posible ??If anyone hawe a ansver for this plz tell me :slight_smile: