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.