I have a very simple script written to allow player to wrap around the screen on the X axis when Q is held down but Y should always be clamped.
What I cannot get my head around is how in the below code Y is not getting clamped while Q is held down.
Y is however getting clamped properly if Q is not held down.
//Move player based on axis input
transform.Translate(new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0) * Time.deltaTime * speed);
//If Q is held down allow horizontal wrapping
if (Input.GetKey(KeyCode.Q) && transform.position.x < -11.2f)
{
transform.position = new Vector3(11.2f, Mathf.Clamp(transform.position.y, -3.8f, 0), 0);
}
else if (Input.GetKey(KeyCode.Q) && transform.position.x > 11.2f)
{
transform.position = new Vector3(-11.2f, Mathf.Clamp(transform.position.y, -3.8f, 0), 0);
}
if(!Input.GetKey(KeyCode.Q) && transform.position.x > -11.2f & transform.position.x < 11.2f)
transform.position = new Vector3(Mathf.Clamp(transform.position.x, -9f, 9f), Mathf.Clamp(transform.position.y, -3.8f, 0), 0);