Moving player a specific amount of pixels.

Howdi, I´ll get right to the point.

I would like to make my character move when I press a button. Though, I would only like to move a specific distance, like 50 pixels for example, so I tried this out:

transform.Translate(Vector3.right + Game.numberToMove);

(numberToMove will contain a value between 100-300).

The console tells me that I can´t use “+” in this case, if I use “*” the player just slides away.
How will I make this work?

Oh, I use C# by the way.

Regards.:smile:

“+=” should work huh??

You will need to know which “pixel” the character is on…

Camera.WorldToScreenPoint will get you which pixel you are on, Then do a addition of Game.numberToMove in the direction you want to go. Your best bet then is to create a plane where the player is, then do a Camera.ScreenPointToRay on the new coordinates and position the player on the intersection to the plane.

Hi, no, this results in the same problem.

Hi, thanks for replying. I suppose this requires my main camera to be located at the same place as the player. The camera is setup like 3rd person view, will this still work?

Regards.

Actually, when you build your “plane” you build it in the Camera.forward direction, not the player direction. This will always move it X pixels according to the camera.

Please bear with me while being a bit slow, I´ve not done a lot of work with planes and all its features yet.

Should I create a plane where my player is located, in the forward direction seen from the camera?

(or, should I go back to Unity essentials?:wink: )

yes…

Plane plane=new Plane(Camera.forward, target.position);

It will be something more like Camera.main.transform.forward… not literally Camera.forward