Input.mouse position

Hey, I am trying to get my player to follow the mouse on the x axis but when I move my mouse on screen the player is always to the right of where my mouse is. I’ve done this before and it worked so I’m not sure whats wrong. In the picture the red marker is where the mouse in during gameplay.

[code=CSharp]using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class movement : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
      
    }



    public float unitTimes = 13.4f;

    void Update()
    {

        float mouse = Input.mousePosition.x / Screen.width * unitTimes;
      

        Vector2 sideways = new Vector2(mouse, -3.69f);

        //sideways.x = Mathf.Clamp(mouse, 0.05f, 13.38f);

        transform.position = sideways;

        //Debug.Log(Input.mousePosition.x / Screen.width * unitTimes);
    }
}

[/code]

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
transform.position = ray.origin;

?

1 Like

thanks works great for movement. Although I am new to code and am trying to get movement on the x only.

1 Like

ray.origin.x?

transform.position = new Vector2(ray.origin.x transform.position.y)
2 Likes

Thanks this helped a lot