Limiting Rigidbody position and rotation without Jitter

Hi everyone;
I’m making a game similar to Star Fox, as of now i’m working on moving the player ship, the ship keeps moving forward during the whole level and the player can move around the x and y axis. As of now i do that by setting the velocity of the spaceship in relation the the player’s input and it works great.

My problem is, the player shouldn’t be moving completely free, it should have boundaries on top and on the sides, also, the player’s ship rotates a bit when it moves and that rotation needs to be clamped as well.

Now mathf.clamp does the job however it’s super bad looking, english is not my native language but i saw many calling this visual effect jitter.

Now i know c# quite well but I really can’t manage to avoid the jitter while also clamping the position and rotation of the player and it’s driving me insane.

Any help is much appreciated!

EDIT: I should also say that i tried using colliders to limit movements in the same way but when i hit those invisible walls the spaceship obviously rotate a bit given the impact and it also slows down due to the attriction which is obviously not what i expect from an invisible wall.

Have you done any Constraint adjustments for the Rigid Body itself? Towards the bottom of the RigidBody component in the Inspector it says ‘Constraints’ which when you click reveals directions in which you can lock the RigidBody! Hope this helps!

Mmh no unfortunately i don’t need to freeze the movement in one of the axis but i need to stop the spaceshipo to go let’s say over y 20

Oh okay! I think I understand where the jitter is coming from. But would have to see the controller code.

I recently wrote a custom First Person Controller with X,Y limitations on the camera dealing with pitch and yaw and my first few iterations had jitter and this was caused because while I was indeed blocking movement with a certain X,Y threshold, the variable in which I was checking to block with was still incrementing/decrementing even though the actual coordinates were locked, which effectively made moving again (once I was at a max distance in any direction) chop to the next available position with respect to how far the incrementer had moved… if that makes sense! lol You are definitely on the right track using Mathf.Clamp but you probably need to reset your pitch and yaw equivalent variable when they max out.

EDIT… EDIT

Place this on a camera to test it out! It is basic movement with locking pitch/yaw and checker functions to make sure there is no jitter once you are in a maxed value!

I forgot the line that actually moves the camera!!! Sorry!! lol It is there now!

(UnityScript)

#pragma strict

public var speedH : float = 4.0f;
public var speedV : float = 2.0f;

private var yaw : float = 90.0f;
private var pitch : float = 0.0f;

private var minY : float = -25.0f;
private var maxY : float = 30.0f;

private var minX : float = -50.0f;
private var maxX : float = 250.0f;


function Start()
{
    Cursor.lockState = CursorLockMode.Locked;
}

function Update()
{
    //update yaw and pitch based on mouse location..
    yaw += speedH * Input.GetAxis("Mouse X");
    pitch -= speedV * Input.GetAxis("Mouse Y");
 
transform.eulerAngles = new Vector3(Mathf.Clamp(pitch,minY,maxY), Mathf.Clamp(yaw, minX, maxX), 0.0f);

    checkClampY(minY, maxY);
      checkClampX(minX, maxX);
}

function checkClampY(min : float, max : float)
{
    if(pitch < min)
    {
        pitch = minY;
    }
 
    if(pitch > max)
    {
        pitch = maxY;
    }
}

function checkClampX(min : float, max : float)
{
    if(yaw < min)
    {
        yaw = minX;
    }
 
    if(yaw > max)
    {
        yaw = maxX;
    }
}