Clamping player to camera is not working to me... need help

Hello guys, I’m trying to make a Rail Space Shooter (like Star Fox, for example), and have been with an issue for a few days.

I’m trying to clamp the player to the camera, so it cannot move arround beyond the camera.

Searching over, I made this two methods in a script assigned to the player GameObject:

void Update(){
        processInput();
        float h = Input.GetAxis("Mouse X");
        float v = Input.GetAxis("Mouse Y");
        MoveArround(h, v, 18);
    }

void MoveArround(float x, float y, float speed)
    {
       
        transform.localPosition += new Vector3(x, y, 0) * speed * Time.deltaTime;
        ClampPosition();
    }

    void ClampPosition()
    {
        Vector3 pos = Camera.main.WorldToViewportPoint(transform.position);
        pos.x = Mathf.Clamp01(pos.x);
        pos.y = Mathf.Clamp01(pos.y);
       
        transform.position = Camera.main.ViewportToWorldPoint(pos);
   
    }

The issue is that if I don’t call the method ClampPosition(), as spected, the player can move beyond the camera. If I call it, the player stays still, and don’t move at all.

A weird thing, is that if I put a debugger log on both methods, de Console show the text on the MoveArround method, the console is showing the message on the log constantly, but the debugger log on the ClampPosition method is only shown once.

Hope someone can help me.

Thank you all!

The console might be set to “collapse” (next to “clear on play”), which makes identical messages get a counter instead of getting reprinted.

The code looks fine. To understand what’s going on, try to print out the values of pos in ClampPosition - what they’re before clamping, and what they’re after.

Thank you Baste! That was the cuestion with the console yes.

When I print the “pos”, I get the values (0.0,0.0,0.0)

If you print it before you clamp, too?