Search Unity

Orbiting Player With Camera

Discussion in 'Scripting' started by steellama, Aug 6, 2018.

  1. steellama

    steellama

    Joined:
    Jun 16, 2018
    Posts:
    1
    I'm trying to make a third person shooter at the moment, and I've run across a bit of a snag when looking for the right camera script.
    I found something good for a first person viewpoint, but it doesn't work for a third person game because when it rotates vertically it won't pivot around the character (on the Y axis) as I'd like it to.
    I'm basically looking for something like the camera in fortnite, tbh.
    The X axis works perfectly, and it'd be great if anyone knew how to integrate a functional orbit for the Y axis too.
    I attached a file with a pic I drew basically outlining what I need it to do lol.
    Thanks in advance!




    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class AlternateMouseLook : MonoBehaviour {
    6.     public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
    7.     public RotationAxes axes = RotationAxes.MouseXAndY;
    8.     public float sensitivityX = 15F;
    9.     public float sensitivityY = 15F;
    10.  
    11.     public float minimumX = -360F;
    12.     public float maximumX = 360F;
    13.  
    14.     public float minimumY = -60F;
    15.     public float maximumY = 60F;
    16.  
    17.     float rotationY = 0F;
    18.  
    19.    
    20.  
    21.  
    22.  
    23.     // Use this for initialization
    24.     void Start () {
    25.        
    26.     }
    27.  
    28.     void Update()
    29.     {
    30.         /*
    31.         if (axes == RotationAxes.MouseXAndY)
    32.         {
    33.             float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
    34.  
    35.             rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
    36.             rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
    37.  
    38.             transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
    39.         }
    40.         else if (axes == RotationAxes.MouseX)
    41.         {
    42.             transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
    43.         }
    44.         else
    45.         {
    46.             rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
    47.             rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
    48.  
    49.             transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
    50.         }
    51.         */
    52.    
    53.         if (Screen.lockCursor == true)
    54.         {
    55.             if (axes == RotationAxes.MouseXAndY)
    56.             {
    57.                 float rotationX = new float();
    58.  
    59.                 if (transform.parent != null && transform.parent.CompareTag("Player") == true)
    60.                 {
    61.                     rotationX = transform.parent.transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
    62.  
    63.                     rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
    64.                     rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
    65.  
    66.                     transform.parent.transform.localEulerAngles = new Vector3(0, rotationX, 0);
    67.                     transform.localEulerAngles = new Vector3(-rotationY, 0, 0);
    68.  
    69.                 }
    70.                 else if (transform.parent != null)
    71.                 {
    72.                     rotationX = transform.parent.transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
    73.  
    74.                     rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
    75.                     rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
    76.  
    77.                     transform.parent.transform.localEulerAngles = new Vector3(0, rotationX, 0);
    78.                     transform.localEulerAngles = new Vector3(-rotationY, 0, 0);
    79.                 }
    80.                 /*
    81.                 else {
    82.                     rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
    83.  
    84.                     rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
    85.                     rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
    86.  
    87.                     transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
    88.                 }
    89.                 */
    90.             }
    91.             else if (axes == RotationAxes.MouseX)
    92.             {
    93.                 if (transform.parent != null)
    94.                 {
    95.                     transform.parent.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
    96.                 }
    97.                 else
    98.                 {
    99.                     transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
    100.                 }
    101.             }
    102.             else
    103.             {
    104.                 rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
    105.                 rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
    106.  
    107.                 transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
    108.             }
    109.         }
    110.     }
    111.  
    112.    
    113. }
    114.  
     

    Attached Files:

  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,778
    Look at Unity Cinemachine. Should solve your concerns.