Help with camera movement constraint

Hello there,

I’ve been working on this for a while as Starfox was my favourite game growing up, and I’m very excited to flesh it out.

I’ve been looking for a better way to control the camera, as i want it zoomed in but able to move sideways/up and down.

The issue is, i can only get about 22 units left or right, and 12 units up or down moving the camera.

I’ve got the x and y range to be 2000

not sure why it won’t keep moving the camera.

    void ClampPosition()
    {
        //stores the objects transform = camera space rather then world space
        Vector3 pos = mainCamera.WorldToViewportPoint(transform.position);
        pos.x = Mathf.Clamp01(pos.x);
        pos.y = Mathf.Clamp01(pos.y);
        
        if (xThrow != 0 || yThrow != 0 )
        { 
            MoveCamera(pos);
            transform.position = mainCamera.ViewportToWorldPoint(pos);
        }

    }

    private void MoveCamera(Vector3 pos)
    {
        Vector3 mainCameraUpdated = mainCamera.transform.localPosition; 
        mainCameraUpdated.z = zCameraPosition;
        Debug.Log("pos " + pos);
        if (pos.x > .85f && xThrow > 0 || pos.x < .15f && xThrow < 0 )
        { 
            mainCameraUpdated.x += xThrow ;
            mainCameraUpdated.x = Mathf.Clamp(mainCameraUpdated.x, -xRange, xRange);
        }

        if (pos.y > .85f && yThrow > 0 || pos.y < .15f && yThrow < 0)
        {
            mainCameraUpdated.y += yThrow ;
            mainCameraUpdated.y = Mathf.Clamp(mainCameraUpdated.y, -yRange, yRange);
            
        }
        mainCamera.transform.localPosition = Vector3.SmoothDamp(transform.localPosition, mainCameraUpdated, ref velocity, smoothTime);

        Mathf.Clamp(mainCamera.transform.localPosition.x, -xRange, xRange);
        Mathf.Clamp(mainCamera.transform.localPosition.y, -yRange, yRange);
    }

Hi Caesura,

It’s great to see that you are challenging yourself. :slight_smile:

From what I understood, you want to be able to zoom and keep the offset to the edges of the screen (viewport). Have you already seen this thread or this? And have you already tried to add Debug.Logs to your code to see what is going on during runtime?

The goal(and it works for about 22 units on the x and 12 on the right) is when i move to the right, the camera is almost"pushed" to the right until it hits the x or y range.

Or vice versa.

This way the camera almost gets"pushed" by the ship.

This is so i can set my camera close to the ship, but not be limited to moving only in that small camera space.

Why does the camera get pushed? That’s not supposed to happen unless you assigned a collider to the game object.

If you work with hard-coded values, you have to test your code and find the right values. Otherwise, you’ll have to develop an algorithm that calcuates the values for you.

    if (pos.x > .85f && xThrow > 0 || pos.x < .15f && xThrow < 0 )
    { 
        mainCameraUpdated.x += xThrow ;
        mainCameraUpdated.x = Mathf.Clamp(mainCameraUpdated.x, -xRange, xRange);
    }

If i move to the side of the screen, it should be updating the camera position.

And it does… but only up to 22 on the x, or -22 (depending on the direction)

And on the y, it only goes to 12 (or -12)

Did you check the value of xRange? During runtime? Since you use the Mathf.Clamp method, that could explain -22 (or 22) and -12 (or 12).

x and y range are both at 2000 currently(way above what i need so as to be obvious if its working or now)

Its very strange how it BEGINS to work, then stops updating after 21-22.

How do you figure out that it is 2000? Did you use a Debug.Log?

The best would be to comment all the lines out and add them one by one. And each time, you test if the code does what you expect it to do. Once it stops working, you found the problem.

Mathf.Clamp returns a value. It does not modify the first parameter. You have to assign the returned value if you want to do something with it. See Rick’s code.

They are serialized fields, been set to 2000 in the inspector.

The code currently doesn’t work as expected.

It will only update the camera 22 units on the x, and 12 units on the y.

Then it stops.

My understanding of what i am showing here, is as long as i am close to the edge of the camera(my pos) my xThrow/yThrow should be added onto the camera position.

update.

looks like SmoothDamp was the culprit.
Not sure why.

Maybe its moving too little before the next update loop happens.

void ClampPosition()
{
    //stores the objects transform = camera space rather then world space
    Vector3 pos = mainCamera.WorldToViewportPoint(transform.position);
    pos.x = Mathf.Clamp01(pos.x);
    pos.y = Mathf.Clamp01(pos.y);
    
    if (xThrow != 0 || yThrow != 0 )
    {
        Debug.Log("pos " + pos);
        MoveCamera(pos);
        transform.position = mainCamera.ViewportToWorldPoint(pos);
    }

}

private void MoveCamera(Vector3 pos)
{
    Vector3 mainCameraUpdated = mainCamera.transform.localPosition;
    mainCameraUpdated.z = zCameraPosition;

    if (pos.x > .85f && xThrow > 0 || pos.x < .15f && xThrow < 0 )
    { 
        mainCameraUpdated.x += xThrow * Time.deltaTime * xControlSpeed;
        mainCameraUpdated.x = Mathf.Clamp(mainCameraUpdated.x, -xRange, xRange);
    }

    if (pos.y > .85f && yThrow > 0 || pos.y < .15f && yThrow < 0)
    {
        mainCameraUpdated.y += yThrow * Time.deltaTime * xControlSpeed;
        mainCameraUpdated.y = Mathf.Clamp(mainCameraUpdated.y, -yRange, yRange);
        
    }
    mainCamera.transform.localPosition =  mainCameraUpdated;
    //mainCamera.transform.localPosition = Vector3.SmoothDamp(transform.localPosition, mainCameraUpdated, ref velocity, smoothTime);

}

Hi @Caesura,

I’m sorry for the late reply. For some reason, I did not get any notification for this thread. :confused:

How are you getting on with this problem?


See also:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms