# About 'Distance As A Vector2'!

#1

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 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!

#2

[Question]

Hello Rick,

I did a little different. Instead of:

I used:

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

#3

[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.

Can you give me a hint about how to solve that?

#4

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.

EDIT:
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 ?
yep,

``````void Start () {
GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Kinematic;
}
``````

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

#5

Hello!

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.

Philippe

#6

Hello!

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