I am making a 2D space shooter mobile game. I am trying to set a boundary so the ship does not go out of the screen. You can move the ship by tilting your phone. I have a shipController.cs attached to my ship object. And this is my FixedUpdate function:
public float xMin, xMax, yMin, yMax;
void FixedUpdate()
{
//tilt to move ship
transform.Translate(Input.acceleration.x * speed, Input.acceleration.y * speed, 0);
//create boundries
rigidbody2D.position = new Vector2(
Mathf.Clamp(rigidbody2D.position.x, xMin, xMax),
Mathf.Clamp(rigidbody2D.position.y, yMin, yMax)
);
}
The min and max values are defined in unity inspector.
This code works great if you are testing on pc. But when exporting to the phone, the movement is very jittery and glitches. The ship will get to the boundary and kind of start jumping. The movement when tilting is also not very smooth. Is there any other way to make this smoother?