Z axis of the ball changes when the ball is made to stick to the paddle

using System.Collections.Generic;

using UnityEngine;

public class ball : MonoBehaviour
{
    [SerializeField] paddle paddle;
    Vector2 diffPos;
    // Start is called before the first frame update
    void Start()
    {
        diffPos = transform.position - paddle.transform.position;
    }

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

WHEN THE CODE GETS EXECUTED THE Z AXIS OF MY BALL CHANGES AND NOT STAYS THE SAME. Why does this happen? But I didn’t change my z axis . It automatically changes when the ball script runs? can you explain why and a solution ?

Hi Allan,

Welcome to our community! :slight_smile:

When you assign a Vector2 object to a Vector3 variable/property, the object gets implicitely converted to a Vector3 object with z = 0. If the z-position of your ball gets set to 0 when you start your game, the reason for that is this line: transform.position = paddlePos + diffPos;.

tq…thats what i exactly needed

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

Privacy & Terms