Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

C# HOW TO LIMIT CAMERA ROTATION

Discussion in 'Scripting' started by Soogbad, Apr 22, 2017.

  1. Soogbad

    Soogbad

    Joined:
    Apr 22, 2017
    Posts:
    8
    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.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CameraRotation : MonoBehaviour
    6. {
    7.     Vector2 mouseLook;
    8.     Vector2 smoothV;
    9.     public float sensitivity = 2.2f;
    10.     public float smoothing = 2.0f;
    11.     GameObject character;
    12.     public GameObject cam;
    13.  
    14.  
    15.     void Start()
    16.     {
    17.         character = this.transform.parent.gameObject;
    18.     }
    19.  
    20.  
    21.     void Update()
    22.     {
    23.         var md = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
    24.        
    25.         md = Vector2.Scale(md, new Vector2(sensitivity * smoothing, sensitivity * smoothing));
    26.         smoothV.x = Mathf.Lerp(smoothV.x, md.x, 1f / smoothing);
    27.         smoothV.y = Mathf.Lerp(smoothV.y, md.y, 1f / smoothing);
    28.         mouseLook += smoothV;
    29.  
    30.         character.transform.localRotation = Quaternion.AngleAxis(mouseLook.x, character.transform.up);
    31.         transform.localRotation = Quaternion.AngleAxis(-mouseLook.y, Vector3.right);
    32.  
    33.     }
    34.  
    35. }
    36.  
     
  2. Soogbad

    Soogbad

    Joined:
    Apr 22, 2017
    Posts:
    8
  3. paulbuck86

    paulbuck86

    Joined:
    Apr 17, 2017
    Posts:
    15
    Try this Camera, it was made by the community a few years back. Might be what you're looking for.
     

    Attached Files:

  4. paulbuck86

    paulbuck86

    Joined:
    Apr 17, 2017
    Posts:
    15
    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.​
     
    Last edited: Apr 23, 2017
  5. Soogbad

    Soogbad

    Joined:
    Apr 22, 2017
    Posts:
    8
    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?...
     
  6. djfunkey

    djfunkey

    Joined:
    Jul 16, 2012
    Posts:
    201
    Just clamp the cameras y rotation at the end of the script
     
  7. Soogbad

    Soogbad

    Joined:
    Apr 22, 2017
    Posts:
    8
    Can you send me a script please? I'm a noob I don't even know what clamp means
     
  8. djfunkey

    djfunkey

    Joined:
    Jul 16, 2012
    Posts:
    201
    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.
     
  9. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    4,440
    This one is in Javascript; it won't help someone who barely know how to code. :)

    @SoogbadYT 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.
     
  10. Soogbad

    Soogbad

    Joined:
    Apr 22, 2017
    Posts:
    8
    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
     
    Last edited: Apr 23, 2017
  11. Soogbad

    Soogbad

    Joined:
    Apr 22, 2017
    Posts:
    8
  12. Soogbad

    Soogbad

    Joined:
    Apr 22, 2017
    Posts:
    8
  13. paulbuck86

    paulbuck86

    Joined:
    Apr 17, 2017
    Posts:
    15
    @SoogbadYT 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.