I’m currently trying to make a simple Atari Breakout game on Unity and have run into a problem with the paddle (player) movement. Despite it not being allowed to move past the x position 9.15 on the right and -9.15 on the left, it goes just slightly over the barrier, as is shown in the following
picture. Instead of stopping at 9.15 at the rightmost limit, it reaches an x position of 9.199995, causing visible spillage over the gray wall on the right side. The same is true for the left side. I’ll attach the code for the paddle which is likely causing the issue here:
public class Paddle : MonoBehaviour
{
public Vector2 speed = new Vector2();
private int movementMult = 1;
// Start is called before the first frame update
void Start()
{
transform.position = new Vector2(0, -4);
}
void onTriggerEnter2D(Collider2D col)
{
if(col.gameObject.tag == "Vertical")
{
movementMult = 0;
}
}
// Update is called once per frame
void FixedUpdate()
{
float inputX = Input.GetAxisRaw("Horizontal");
Vector2 movement = new Vector2(speed.x * inputX * movementMult, 0);
movement *= Time.fixedDeltaTime;
if(transform.position.x >= 9.15 && inputX == 1)
{
movement.x = 0;
}
else if(transform.position.x <= -9.15 && inputX == -1)
{
movement.x = 0;
}
transform.Translate(movement);
}
}
The onTriggerEnter2D method uses the tag “Vertical” to represent collisions with the vertical “walls” on either side of the screen, which are positioned at the gray sections on either side. The colliders are placed perfectly by zooming in extremely far, so I don’t see how those could be an issue.
I honestly don’t understand how these if statements are failing to halt the paddle exactly at its intended point. I don’t know what to try to attempt to remedy this. What would be a good method to increase the precision at which I can control this collision?