(Why) Are we not sticking the ball to paddle on the Y axis?

Hi,

Not sure if i’m missing something, but this script doesn’t seem to stick the ball to the paddle on the Y axis, only the X axis ?

With the script below the ball stays at the height where I placed it, but does follow the paddle on X axis.

How would you make it stick to the paddle on Y axis as well ?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Ball : MonoBehaviour
{

    // config params
    [SerializeField] Paddle paddle1;

    // state
    Vector2 paddleToBallVector;

    // Use this for initialization
    void Start()
    {
        paddleToBallVector = transform.position - paddle1.transform.position;
    }

    // Update is called once per frame
    void Update()
    {
        Vector2 paddlePos = new Vector2(paddle1.transform.position.x, paddle1.transform.position.y);
        transform.position = paddlePos + paddleToBallVector;
    }
}

Hi Mellow,

We don’t want to have our ball inside the paddle, thus we need an offset on the y-axis.

Did this clear it up for you?

I understand that we need an offset, but we are only using the X-axis from that offset to position the ball.
The Y axis position remains unchanged.
(So I have to manually drag the ball to the correct height)

Since we already are positioning the ball on X through script, it would make sense to me to also
adjust the ball to the correct height on Y via that script.

So what would I need to add to also change the balls Y-position ?

Hope that makes sense.

That’s right. We want to be able to define the offset in the scene editor.

That’s a matter of personal preference. If you know the “correct” height, you can easily do that. Modify the y-component of paddleToBallVector or assign a value to paddleToBallVector directly.

This is the line where the program calculates the offset:

paddleToBallVector = transform.position - paddle1.transform.position;
1 Like

I still don’t know what to do.

paddleToBallVector is already returning the X, and Y value.

If I Debug.Log on the paddleToBallVector it shows both X and Y values.

And we are defning the paddlePos variable with an X and Y value as well.

So why does transform.position = paddlePos + paddleToBallVector not update position on Y ???

Question remains;

How can I include the position on Y axis when positioning the ball through script ?

If anyone could just give me the literal code would be great, because I just can’t figure this out.

What do you mean by this?

I tested the code, and it does update the y axis, if I move the paddle in the Y axis the ball will also move along.

ezgif-6-c73b56cb7660

Or is this what you are trying to do?
ezgif-6-42987d79866c

If that’s the case then you’ll have to calculate that position, a very easy way to do that is by setting an empty child object to the paddle, then set the position of the ball to that child’s position. That should be done before doing the rest of the calculations.

    void Start()
    {
        transform.position = paddleChild.position;

        paddleToBallVector = transform.position - paddle1.position;
    }

Something like that.

Actually this script does not “stick” it to either X or Y at least not automatically. What it does do is maintain whatever vertical / horizontal offset/gap you gave it in the designer when the game starts.

So using this script on the ball you need to make sure you manually drag the ball it to whatever suitable position on the paddle you want it to stay when the game starts while in edit mode itself. And the script will maintain that offset between the ball and paddle , making it look like the ball is stuck to he paddle when you move the paddle. But this means If you leave a gap either vertically or horizontally at design time the script is just going to maintain that gap in game as well.

Thats exactly what I was trying to do !

I actually did not realize it was already adjusting the ball on Y, as I have only been moving the paddle left and right. (doh !)

So I understand my question must have been very stupid @Nina :), thanks for you patience.

However this has been most educational.

Thanks x 100000 @Codermonk and @Yee

2 Likes

Don’t worry, @Mellow. Your question was not stupid at all. I’m glad everything is finally working. :slight_smile:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms