Hello. I am trying to make the player move towards the mouse using ScreenToWorldPoint , and I am trying to make the player move along the X-Axis ONLY. I have a script that already does this (a different way of doing it without ScreenToWorldPoint) and it works, I am looking to do it a little bit differently though. Right now with this script nothing happens. The player does not move. Any ideas?
Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 movePos;
movePos = new Vector3(pos.x, 0, 0);
transform.position = Vector3.Lerp(transform.position, movePos, ScrollSpeed * Time.deltaTime);
One possible issue with your code is that you are setting the y value of the movePos vector to 0, which means that the player will not move along the y-axis at all. If you want the player to move only along the x-axis, you can set the y value of movePos to the current y value of the player’s position, like this:
Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 movePos;
// Set the y value of movePos to the current y value of the player's position
movePos = new Vector3(pos.x, transform.position.y, 0);
transform.position = Vector3.Lerp(transform.position, movePos, ScrollSpeed * Time.deltaTime);
This should make the player move along the x-axis only, while keeping the player’s current y position.