Why Don't we use the New Vector to create paddleToBallVector

When creating the functionality to ‘stick’ the ball to the paddle, we create two Vector2 variables. In a past example and for the paddlePos vector, we use the new Vector2 keyword but don’t in the paddleToBallVector (but it still worked). Just wondering if new Vector2 is required after declaring it a Vector2, and if so, why does it work in the lecture code?

public class Ball : MonoBehaviour
{

[SerializeField] Paddle paddle1;

Vector2 paddleToBallVector;

void Start()
{
    //Why don't we declare a new Vector2 here???
    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,

The new Vector2 is necessary because we create a new Vector2 object. Vector2(something, something) does not work because Vector2 is not a method but a struct. new Vector2 is a constructor call.

Is this what you wanted to know?

Yes, thankyou! That makes sense.

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

Privacy & Terms