Limiting paddle position

i’m experimenting with moving the paddle by using Input.getAxis(“Mouse X”) the result is a paddle the can be control even when the cursor is locked. but i’ve run into a bit of a problem limiting the horizontal movement of the padel to the left and right positions of the screen.

using UnityEngine;

public class paddle : MonoBehaviour {

    public float mousePos;
	// Use this for initialization
	void Start () {

        Cursor.lockState = CursorLockMode.Locked;
       // Cursor.visible = false;
	// Update is called once per frame
	void Update () {

        mousePos = transform.position.x;
        if(mousePos > -9.3f || mousePos < 7.9f)
            transform.Translate(Input.GetAxis("Mouse X") / 2, 0, 0);

should i assign my axis value to the mousePos variable?

Firstly, you want to set mousePos to include the movement. Basically you want to make sure that the combined movement + current position is a valid move
float mousePos = transform.position.x + (Input.GetAxis("Mouse X") / 2);

For the if, you want to switch the or to an and or else it will always be true. I also played around with the boundaries
if(mousePos > 0.5f && mousePos < 15.5f)

Those two changes should put you in the right direction.

Privacy & Terms