How to clamp the camera rotation

I would like to know how do i clamp the camera rotation in this situation.
I’ve tried so many combinations but i’m stuck… can’t get it figured out…

Basically, i have a touch zone that return me a drag delta ( as a Vector2D ) whenever i swipe in the zone.
I just add that drag delta to the camera angle every frame to have a mouse look style script.
But how i clamp the pitch between -30 and 30 for example?

Here’s the my current code in the Update function

void Update(){

// add the y delta to the camera ( up and down rotation )
playerCamera.transform.eulerAngles += new Vector3 (delta.y, 0.0f, 0.0f);

// add the x delta to the PLAYER ( empty object with camera as child )
transform.eulerAngles += new Vector3 (0.0f, delta.x, 0.0f);

}

I have already tried something like this… it clamp the vertical, but there’s no point if i can’t look horizontal anymore…

void Update(){
pitch+=delta.y
pitch=Mathf.Clamp(pitch,-30,30);

// set this every frame, so you can only look forward once the scene start                            
playerCamera.transform.eulerAngles = new Vector3 (pitch, 0.0f, 0.0f);

// this overlaps with the line above
transform.eulerAngles += new Vector3 (0.0f, deltaCm.x , 0.0f);

}

Thanks in advance

I think the built in mouseorbit.js has code to clamp rotation

“Why not bother looking it up in the Google. I am sure another duplicate question will be fine!”

Dude, I literally answered this question yesterday! Sigh

Use the Mathf.Clamp. You can find out how to use it in the documentation.

The trick is to keep a reference for both pitch and yaw externally.

private void Update() {
    pitch += delta.y;
    pitch=Mathf.Clamp(pitch,-30f,30f);
    yaw += delta.x;
    yaw = Mathf.Repeat(yaw, 360f); // Keep between 0 and 360 if you want.

    playerCamera.transform.eulerAngles = new Vector3 (pitch, yaw, 0.0f);
}

“Why should i read all through the post, i am sure this is a duplicated question”.
I don’t seem to find a way where Mathf.Clamp should work, if you could read all the post, you will have noticed.

That would work if i had only one object ( the main camera ) to rotate. Since i have a camera for pitch and an empty for yaw, this would not work, as adding another line such as
playerEmptyObject.transform.eulerAngles= new Vector3(0.0f,yaw,0.0f);
get in conflict with the player camera rotation.

Just ignore yaw in the playerCamera and use it in the empty one.

As i said, someway it gets in conflict with the camera pitch… it gets shaky and buggy.
By the way, i removed the empty object for now and applied all directly to the camera…