I am really enjoying making the switch to Unity. But here I have found one of those rare moments where it appears to make things more difficult (at least it is for me as I am crap at understanding the actual manual)

Basically my 'player' is a space ship. It starts life at 0,0,0 world coordinates and I have been doing all the movement based off of that. (ie. Add force X to move left or right, and there is a constant Z speed which is done via rb.transform.Translate().)

I want to restrict the movement on the X Axis, so in Libgdx id use something like:

if (player.position.x <= 0){ player.position.x = 0;}
if (player.position.x >= viewport.getX()){ player.position.x = viewport.getX();}

The thing is that wont work for me in Unity for several reasons (1. I dont know much about the viewport as I didnt create it. 2. the player.position is being affected by the rigidbody forces. AND i am working in world coordinates with the players position).

As I said, the player is constantly moving forward on Z axis, so making colliders doesnt seem like the right choice for me.

I'm pretty sure Unity devs would of put this in Unity v0.001 but I just cannot for the life of me find how to actually do it. (Any info I can find seems from like 2011 and also is pretty badly written code making this simple matter takes dozens of lines of code and some methods - I'm hoping there is a Unity method already made, like player.transform.ScreenPosition or something but yeah I cannot find it!?

Any help would be massively appreciated as always.

  • 1
    \$\begingroup\$ Based on your description, I'm guessing top down 2d? \$\endgroup\$ – MichaelHouse Aug 7 '17 at 23:21
  • \$\begingroup\$ ahh, sorry! Its top-down 3d . Hence the Z axis bit :) But the camera only stays at one height and purely follows the player forward. But the player can dip and rise to some degree and also go left and right \$\endgroup\$ – Big T Larrity Aug 7 '17 at 23:23
  • 1
    \$\begingroup\$ I assumed classic arcade moving toward the top of the screen. Does the camera move? \$\endgroup\$ – MichaelHouse Aug 7 '17 at 23:25
  • \$\begingroup\$ @Byte56 soz it is a terribblee description i gave. Its like the old style arcade topdown shooter, but using 3d models. The camera only follows the player along Z axis. The background is two planes, that roll over when they have been passed. But I cant restrict the right-left movement properly \$\endgroup\$ – Big T Larrity Aug 7 '17 at 23:26
  • 1
    \$\begingroup\$ OK, so can't you set the X bounds based off the camera X position? Like no more than 8 units from the camera's X position at any time? Camera.main.transform.x is the "center" of your view \$\endgroup\$ – MichaelHouse Aug 7 '17 at 23:27

You can restrict the player position to the viewport like this:

void Update() {
    Vector3 pos = Camera.main.WorldToViewportPoint(transform.position);
    pos.x = Mathf.Clamp01(pos.x);
    transform.position = Camera.main.ViewportToWorldPoint(pos);

That converts the position of the player (assuming or any object this script is attached to) into viewport coordinates, which range from 0 to 1. Then clamps the position between 0 and 1. Finally converting back to world space. So, if the object is outside of the screen, its position will convert to something less than 0 or greater than 1, which this script will correct. Otherwise, it'll leave the position where it is.

| improve this answer | |
  • \$\begingroup\$ thanks again. This has helped me understand another little piece of Unity and coding in general. \$\endgroup\$ – Big T Larrity Aug 9 '17 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.