Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

1st person Camera to 3rd person

Discussion in 'General Discussion' started by Luke3671, Oct 21, 2014.

  1. Luke3671

    Luke3671

    Joined:
    Jul 6, 2014
    Posts:
    56
    Heya,
    I using Unity 1st person Camera but trying to make it 3rd person.
    I have moved the Camera back so you can see the "Object" But the problem i'm having is the Camera still thinks it's "1st person" I've removed the "Mouse Look -360 / 360" but that doesn't do nothing is there something i'm missing that needs to be removed/update? Is their a more simple way to fix this little problem?

    Thank you
    Luke
     
  2. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    I did this before. All first person camera controls were in 1 function and the 3rd person camera controls were another function. Have some variable to tell which should be called and use an if statement in your update function to decide which function to call.

    If it looks like pieces of both are running, then there's either an error in your third person script that is doing what the first person script does or somewhere you're calling both.

    --edit

    I should mention I used my own scripts for camera movement since I didn't want to worry about running into a problem JUST like this by modifying the sample.
     
    Luke3671 likes this.
  3. Luke3671

    Luke3671

    Joined:
    Jul 6, 2014
    Posts:
    56
    Yer, I think i might do the same tbf. Thank you 4 your help tho.
     
  4. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    If you skim through the camera code, you'll notice you don't need all of what's there. Here's what I pulled out to get a functional camera :D If you want to walk around, make movement based on a body. If you want to fly or swim, make movement based on the camera position. Good luck!

    Code (CSharp):
    1. Camera head;
    2.  
    3.     //camera related
    4.     float rotation_x = 0;
    5.     float rotation_y = 0;
    6.     float h_speed = 2;
    7.     float v_speed = 2;
    8.    
    9.     float minX = -360;
    10.     float minY = -90;
    11.     float maxX = 360;
    12.     float maxY = 90;
    13.  
    14.     public void init()
    15.     {
    16.         head = Camera.main;
    17.     }//init
    18.  
    19.     public void playerUpdate()
    20.     {
    21.         camControls();
    22.     }//playerUpdate
    23.  
    24.     void camControls()
    25.     {
    26.         //get input
    27.         rotation_x += Input.GetAxis("Mouse X") * h_speed;
    28.         rotation_y += Input.GetAxis("Mouse Y") * v_speed;
    29.        
    30.         //fix input
    31.         if(rotation_x > 360 || rotation_x < -360)
    32.         {
    33.             rotation_x = 0;
    34.         }
    35.        
    36.         if(rotation_y > 360 || rotation_y < -360)
    37.         {
    38.             rotation_y = 0;
    39.         }
    40.        
    41.         rotation_x = Mathf.Clamp(rotation_x, minX, maxX);
    42.         rotation_y = Mathf.Clamp(rotation_y, minY, maxY);
    43.        
    44.         //apply rotation
    45.         head.transform.localRotation = Quaternion.AngleAxis(rotation_x, Vector3.up);
    46.         head.transform.localRotation *= Quaternion.AngleAxis(rotation_y, Vector3.left);
    47.     }//camControls