Can't figure out how to get the transform position I want

I have my paddle set to where I want it but whenever I click play the transform moves which is expected when I set my bool to true however why is it also changing the Y and Z axis when I have it set to false.

Any help is very much appreciated.

Untitled Untitled2

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

public class Paddle : MonoBehaviour
    // Config Params

    [SerializeField] float xMin = -8.5f, xMax = 8f;
    [SerializeField] float DistanceZ = 8.75f, DistanceY = -9.5f;

    // States

    [SerializeField] bool UseInitial_CameraDistance = false;

    private float Actual_DistanceZ;
    private float Actual_DistanceY;

    // Start is called before the first frame update
    void Start()

    // Update is called once per frame
    void Update()


    public void Paddle_Movement()

        Vector3 Mouse_Position = Input.mousePosition;
        Mouse_Position.z = Actual_DistanceZ;
        Mouse_Position.y = Actual_DistanceY;

        transform.position = Camera.main.ScreenToWorldPoint(Mouse_Position);

        var pos = transform.position;
        pos.x = Mathf.Clamp(transform.position.x, xMin, xMax);
        transform.position = pos;


    public void SetCameraDistance()

        if (UseInitial_CameraDistance)
            Actual_DistanceZ = (transform.position - Camera.main.transform.position).magnitude;
            Actual_DistanceY = (transform.position - Camera.main.transform.position).magnitude;
            Actual_DistanceY = DistanceY;
            Actual_DistanceZ = DistanceZ;


Hi David,

Add more Debug.Logs to your code to narrow down the issue. For instance, what is your mousePosition, what does ScreenToWorldPoint return?

Thanks for the reply Nina, My mousePosition doesn’t appear to be affecting the Y and Z axis which should be the case since I thought I locked in those values the only weird thing I saw is that it’s returning a 0 for the z Axis no matter where I look.

My ScreenToWorldPoint is returning close to my locked in numbers the Y axis is -9.5 and the Z axis is showing up as 8.8 which isnt so bad even though its set to 8.75. So that isnt changing the transform position either as far as I know.

I understand that using ScreenToworldPoint, will change the screen into world units I’m just not sure on the way the math works for it, I’ve tried many upon many numbers to get the paddle in game to look like the one in the Scene to no avail I’ll get close for one of them.

Example being I manage to get the Y axis closer to -9.5 by typing in some obscure number like -200 However when I actually reach -9.5 and go to start on the Z axis by changing the Z axis it starts changing the Y axis again.

The mousePosition value makes sense. (0, 0, 0) is in the bottom left corner of your game screen. The values inside the parentheses represent pixel coordinates. If your game screen is 800 pixels wide, the maximum x value would be 800. If your game screen is 200 pixels high, the maximum y value would be 200.

Instead of hard-coding xMin and xMax, I’d recommend to use ViewportToWorldPoint to get the edges of your screen in World Space coordinates. (0, 0) is in the bottom left corner, (1, 1) in the top right corner.

For example:

Camera cam;
float xMin, xMax;

void Start ()
    cam = Camera.main; // cache the reference to improve the performance of your code

    Vector3 bottomLeftCorner = cam.ViewportToWorldPoint(new Vector3(0f, 0f, cam.nearClipPlane));
    Vector3 topRightCorner = cam.ViewportToWorldPoint(new Vector3(1f, 0f, cam.nearClipPlane));

    xMin = bottomLeftCorner.x;
    xMax = topRightCorner.x;

I did not test it, so it’s advisable to log the values into your console if the code does not work as expected.

My apologize Nina, I am still quite new to this so i’m not sure how to use this to help with stopping my paddle’s transform from moving upon pressing play.

I suspect that your xMin and xMax values might be “wrong”. Have you already tried to pass on the unclamped pos value to transform.position?

Yes, the paddle’s Y and Z axis still moves to an undesired location when i run the program unclamped.

Comment out the lines where you assign something to transform.position to see if the issue is really in your Paddle_Movement method or if something else is moving your paddle.

The issue has to due when im assigning the transform to world units with “transform.position = Camera.main.ScreenToWorldPoint(Mouse_Position);”. I can assign the transform to just “Mouse_Position” and the Y and Z axis will stay where I tell it to but then the movement of the paddle doesnt work correctly.

The clamping of the x axis will work however I can only play the game in the lower left corner of the play screen otherwise the paddle will just jump around.

My camera is rotated on the Y axis if that has anything to do with it?

My camera is rotated on the Y axis if that has anything to do with it?

I don’t know. The best would be to write down the expected values and check if your code produces the expected values. For example, what exactly does Camera.main.ScreenToWorldPoint(Mouse_Position) return?

You can move your paddle manually and take the position values from your Inspector as the expected values.

At what position is the PlaySpace game object?

The expected values wanted for the paddle is -9.5Y and 8.75Z, Camera.main.ScreenToWorldPoint(Mouse_Position) is suppose to return the vector 3 of mouse position but in world space buts it’s not, instead its returning -1.4Y and 2.1Z. The playspace is just an empty transform set on 0,0,0 It’s used just to make the hierarchy look cleaner.

I made an offset variable and its close to the position I want now, when maximized on play. So it’s a temp fix I guess but it’s not really useful because if the screen size changed the paddle moves to a different spot again. Example being if im not maximized on play. Another reason why this doesnt really work is because if I want to change the way the game looks again i need to adjust the new variable.

  if (UseInitial_CameraDistance)
        Actual_DistanceZ = (transform.position - cam.transform.position).magnitude + Paddle_OffsetZ;
        Actual_DistanceY = (transform.position - cam.transform.position).magnitude + Paddle_OffsetY;

but yeah i guess its fine for now so i can atleast play test some things, I need to figure out a way so that it can adjust its self by code though for when the resolution changes or if i want to adjust the way the game looks again.

I’m wondering if the issue could be a misunderstanding. Your camera is set to “perspective”, isn’t it? If so, check, where exactly -1.4Y and 2.1Z are. Here is a rough sketch of what I mean.


Given that the green line between the two red lines is the screen size, a parallel green line going through the paddle would be longer.

If my theory is correct, you’ll have to multiply the converted mouse coordinates by 1.x. The field of view value has something to do with the angle.

Multiplying is helping with the Z axis but it makes the Y axis very low, I found dividing helps the Y axis but makes the Z axis to short.

My graphic visualises the z-axis. The value for the y-axis might be different. You probably have to multiply the z and y separately with a unique value each.

The height of the camera is fixed, so calculating the value for the y-axis should not be as difficult as calculating for the z-axis. Screen.width changes depending on the aspect ratio.

I might switch to button movement if I can’t figure it out.

I found a fix for it just like we clamp the Xaxis with mathf.clamp I also clamped the Y and Z axis now no matter the screen size the paddle stays in the same spot as the scene screen.

The only issue that arouse from this that wasn’t happen before is that when my ball hits a block it slows down fast, after a couple hits it stops moving which wasn’t happening before I clamped the paddle on the Y and Z axis.

Although i dont quite understand what the two would have in common.

Do you change the clamping values multiple times while playing your game or just at the beginning?

Ah it’s under paddlemovement which is in my update function so its constantly doing it i suppose.