C# HOW TO LIMIT CAMERA ROTATION

I know everyone will probably say just go on youtube theres lots of videos on that. I know but I was searching for hours and I can’t find one for my camera rotation script and I’m using it because it’s the only one I found that you move where you look at. Anyways, any kind of help would be really appricieted. Thx! Oh only vertical btw.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraRotation : MonoBehaviour
{
    Vector2 mouseLook;
    Vector2 smoothV;
    public float sensitivity = 2.2f;
    public float smoothing = 2.0f;
    GameObject character;
    public GameObject cam;


    void Start()
    {
        character = this.transform.parent.gameObject;
    }


    void Update()
    {
        var md = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
        
        md = Vector2.Scale(md, new Vector2(sensitivity * smoothing, sensitivity * smoothing));
        smoothV.x = Mathf.Lerp(smoothV.x, md.x, 1f / smoothing);
        smoothV.y = Mathf.Lerp(smoothV.y, md.y, 1f / smoothing);
        mouseLook += smoothV;

        character.transform.localRotation = Quaternion.AngleAxis(mouseLook.x, character.transform.up);
        transform.localRotation = Quaternion.AngleAxis(-mouseLook.y, Vector3.right);

    }

}

hey

Try this Camera, it was made by the community a few years back. Might be what you’re looking for.

2529879–175513–WowCamera.cs (4.47 KB)

Let us know if that helped you or not. Any other problems, please let us know.

Also, I use that script attached to a Camera Rig like so:

Camera Rig (Just Transform, adjust positioning of Camera)
Pivot (Just Transform, adjust “Y” value on Position)
MainCamera (Transform, Camera, GUI Layer, Flare Layer, Audio List, Script)
Can also adjust Z and X Position on Main if desired.

I don’t know, it sounds too complex… I dunno if it’ll work with my movement script. Can someone just send me the script I sent but with limits?..

Just clamp the cameras y rotation at the end of the script

Can you send me a script please? I’m a noob I don’t even know what clamp means

No ones just going to send you a complete script…
View here to have some tutorials to help you out, and searching “unity3d limit camera rotation” in google, the first 3 results solved the issue you have. There is also this script on the Unity3D Wiki which would probably help.

This one is in Javascript; it won’t help someone who barely know how to code. :slight_smile:

@Soogbad You definitely need to learn how to script before launching yourself in a project. There are scripting tutorials available here: https://unity3d.com/learn/tutorials/s/scripting I strongly recommend that you make all of them in the order of the page.

The project tutorials @djfunkey suggested will help you understanding how Unity works too.

You absolutely didn’t listen to what I said… I just want to edit the script that I sent here and add limits to it… I’ts probably about to lines not a complete script… And no, I don’t want tutorials cuz I wanna use this script which is the only one I found in which you walk where you look at… That other guy said something about clamping… EDIT: oh lol it was you haha how did I not notice

Thx. I’ll check these out.

Hey

@Soogbad That script given to you above will do exactly what you asked. If you don’t want the whole script, open it up and it’s well commented. Extract what you need and add it to your script.