# Player going out of boundaries

So, I been following up the Space shooter tutorial on Unity, I had an issue where the project was acting up with an alpha version, so I was able to get a version that worked on a old release and applied all the new things I developed

But suddenly, my ship is acting up when getting closer to the boundary of its movement

This is the player movement code

public class playerMovement : MonoBehaviour
{
public float speed = 0.0f;
public float tilt;
public Boundary boundary;

void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");

var movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
var rigidBody = GetComponent<Rigidbody>();
rigidBody.velocity = movement * speed;

rigidBody.position = new Vector3
(
Mathf.Clamp(rigidBody.position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp(rigidBody.position.z, boundary.zMin, boundary.zMax)
);

rigidBody.rotation = Quaternion.Euler(0.0f, 0.0f, rigidBody.velocity.x * -tilt);
}

}

[System.Serializable]
public class Boundary
{
public float xMin, xMax, zMin, zMax;
}


And this is the player boundary properties

It used to work previously moving forward to the alpha, now I'm working on 2019.2.0f1, but since working on the alpha this is the behaviour I'm having

As you can see, getting closer to the player boundary, takes the player out of the play area, and I can't determine why this might be happening.

I already tried removing the Math.Clamp, as I though there migth be bug with the function in this version, but even without it, the problem is present.

Let's step through this, assuming your boundary is at 10, your speed is 50, and your fixed timestep is the default 0.02 s. You're holding the stick to the right:

1. You're at x = 9 when FixedUpdate runs

2. You clamp the position. It's less than 10 so nothing changes.

3. The physics step runs. It moves the object 50 * 0.02 = 1 unit to the right. Now it's at x = 10.

4. The frame is rendered with x = 10. So far so good.

5. You're still at x = 10 when the next FixedUpdate runs.

6. You clamp the position. It's exactly at the boundary so nothing changes.

7. The physics step runs. It moves the object 50 * 0.02 = 1 unit to the right. Now it's at x = 11. Remember, your clamping happened before the physics step, so we don't get a chance to fix this until next FixedUpdate.

8. The frame is rendered with x = 11, out of bounds.

9. You're at x = 11 when the next FixedUpdate runs.

10. You clamp the position, snapping to x = 10.

11. The physics step runs. It moves the object 50 * 0.02 = 1 unit to the right. Now it's at x = 11 again.

12. We'll stay at x = 11 in every subsequent rendered frame until we back off the input.

So, if you want your physics integration to respect your boundaries, make them visible to the physics engine: add a collider to represent the boundary (you can have it absorb all bounce energy so you don't ricochet off of it, and use layers to ensure it doesn't interact with other scene content), or use a joint constraint to limit the physics motion of the object.

• Actually, I'm trying to do this, using a Quad to set the boundary of movement of the player, so for this, I removed the Math.Clamp completely, and the issue is still present – CJLopez Aug 10 at 2:17
• A quad is not an appropriate object for this purpose. A box collider with some depth to prevent tunnelling might suit your needs. – DMGregory Aug 10 at 2:22
• will try to use a box then – CJLopez Aug 10 at 5:22

Are you using Rick's udemy course? It's a great one!

my question is, what is the rigidbody.rotation part about?

also, your problem could be this.

Your supposed to use: Rigidbody2D, not Rigidbody. Rigidbody is used for 3D, while Rigidbody2D is used for, well, 2d.

hope this helps.

• 1. The course I'm following is linked, and its the one provided by unity themselves, so, not sure if its the same 2. It's a 3D game, in a 2.5D style, I'm not using sprites, I'm using models (or will be using, currently, just using simple shapes), that's why I'm using 3d versions of the rigidbody and rotation – CJLopez Aug 10 at 16:28

So, in the end, the culprit was a box collider I use to do memory management, taking out my player when it gets near to it

The killBox is a box collider with which I deactivate objects that reach to it in order to maintain a certain ammount of memory dedicated to bullets and hazards

So, making this object bigger, to better envolve my play area, well, solved my issue. I tried make it fit more properly the play area, and given I worked on something else, never though this might had caused an issue here, so once again, I made it bigger and my problem was solved