Clamp is not working and its causing the "player ship" to teleport

void ProcessTranslation()

{

    float xThrow = Input.GetAxis("Horizontal");

    float yThrow = Input.GetAxis("Vertical");

    float xOffset = xThrow * Time.deltaTime * controlSpeed;

    float rawXPos = transform.localPosition.x + xOffset;

    float yOffset = yThrow * Time.deltaTime * controlSpeed;

    float rawYPos = transform.localPosition.y + yOffset;

    float clampedXPos = Mathf.Clamp(rawXPos,-xRange, xRange);

    float clampedYPos = Mathf.Clamp(rawYPos,-yRange, yRange);

    transform.localPosition = new Vector3(

        clampedXPos,

        clampedYPos,

        transform.localPosition.z

    );

}

Whenever I run this code snippet in my playerControls script, the player teleports to weird coordinates. If I use rawXPos instead of ClampedXPos and same for Y axis, then the code works fine but without the clamps. How do I make the clamps work without my ship teleporting to weird spots? Please help.

Hi Siddharth,

Welcome to our community! :slight_smile:

Good job on analysing the problem. Since the issue does not seem to appear when you do not clamp the position values, it is very likely caused by the clamp values defined in the Inspector.

Keep your game window open while the game is not running. Move the spaceship manually in the scene window until it reaches an edge of your game window. Check the position of the ship. That’s the minimum/maximum local position you want to allow for the ship, so it does not leave the visible area in your game window.

Alternatively, you could use rawXPos and log the coordinates into your console to see what values you are getting at runtime. If clampedXPos contradicts those values, something is wrong with the value of xRange. In that case, you could use the maximum value, you figured out with rawXPos.

Both approaches are valid.

Did this help you fix the problem?


See also:

Also, since you’re using localPosition, make sure the parent is at (0,0,0)

1 Like

I would have to hard-code the values in this solution, right? Isn’t there any other method where I can clamp the values relative to the ship’s position?

In this project, we do indeed hard-code the values.

However, there is also a way to calculate the allowed range, for example, with the Camera.ViewportToWorldPoint method. It’s not difficult but it requires you to learn a bit by yourself and to test your attempts. This method could tell you the world position of a point, for example, on the left edge of the viewport, or the top edge, and so on. With that position, you could caclulate the offset from the centre of the camera viewport to the edges of the viewport. The (absolute) offset value is the range.

If you want to calculate the range values at runtime but cannot figure the solution out yourself, please feel free to ask our helpful community of students for advice over on our Discord chat server.

Good luck! :slight_smile:

1 Like

Ah okay, I’ll try out the Camera.ViewportToWorldPoint approach. Thank you very much for the help!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms