Search Unity

Having rough time with camera controller

Discussion in 'Scripting' started by Mozepo123, Jun 19, 2019.

  1. Mozepo123

    Mozepo123

    Joined:
    Jun 9, 2019
    Posts:
    2
    Sorry if I posted in the wrong section.
    Hi guys, I've just started with unity, and still a big noob in C#, how can I fix the camera behind character's back, because right now after I rotate the camera it returns to the initial point without actually taking into action characters rotation at all, so if I, let's say, rotate character 180 degrees my character will face the camera as it stays in the very same spot without getting behind his back.
    Here's the rough code I've made for the camera controller:



    Code (CSharp):
    1.  
    2.     > public Vector3 campos;
    3.     >      void Start () {
    4.     >    
    5.     >             campos = new Vector3(0.0f, 0.0f, 0.0f);
    6.     >         }
    7.     >        
    8.     >        
    9.     >         void Update () {
    10.     >    
    11.     >    
    12.     >      if (Input.GetMouseButton(2))
    13.     >             {
    14.     >                 yaw += speedH * Input.GetAxis("Mouse X");
    15.     >                 pitch -= speedV * Input.GetAxis("Mouse Y");
    16.     >    
    17.     >    
    18.     >              
    19.     >            
    20.     >                 transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);
    21.     >              
    22.     >              
    23.     >             }
    24.     >             else
    25.     >             {
    26.     >    
    27.     >            
    28.     >                 transform.eulerAngles = campos;
    29.     >                
    30.     >             }
    31.  
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,195
    I don't recommend trying to make your own camera controller. It's a lot of work, and it often deals with some pretty complicated math to get things to work right.

    I'd recommend using Cinemachine, which is free and part of Unity in the Package Manager. It's pretty easy to set up some simple stuff, and there's a lot of decent documentation and videos on it.

    One thing you could do is just make the camera a child of the character, so that it turns as the character turns. Not sure if that's reasonable in your case.
     
    Mozepo123 likes this.
  3. Mozepo123

    Mozepo123

    Joined:
    Jun 9, 2019
    Posts:
    2
    I guess I will give it a try. Thanks!