About Ball Following The Paddle

cud_s05_distance_as_a_vector2

#1

So I think I’ve done the exact same but for some reason my code doesn’t make the ball follow the paddle, it just changes where the ball is at the start and then nothing happens. But when I copy the codes in the lecture it works.
Here are my codes what did I do wrong and what causes that problem ?

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

public class Ball : MonoBehaviour
{
    // configuration
    [SerializeField] Paddle paddle1;

    // state
    Vector2 paddleToBallVector;

    // Start is called before the first frame update
    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;
    }
}

Are my codes not respecting me because I am not experienced :thinking:


#2

Hi Fatih, welcome to the community :slight_smile:

You have a typo in the last line of your Uodate statement, that second equals sign should be a plus sign. You are adding the paddleToBallVector to the paddle position in order to keep them aligned.

Hope this helps.


See also;