Search Unity

Creating mouse controlled car camera (New to unity)

Discussion in 'Scripting' started by ebob91, Mar 2, 2015.

  1. ebob91

    ebob91

    Joined:
    Mar 2, 2015
    Posts:
    1
    Hello, i am new to unity and i am trying to create a camera that rotates around the car along the Y and X axis.
    I have put the MainCamera as a child of my car, so it follows the car arround.

    Rotating the camera around its own axis works, but i dont know what to do to make the camera rotate around the car.

    This is how i rotate the camera around its own axis:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class OrbitCamera3 : MonoBehaviour {
    5.  
    6.     public float sensitivityX = 5f;
    7.     public float sensitivityY = 5f;
    8.  
    9.     public float minimumX = -100f;
    10.     public float maximumX = 100f;
    11.  
    12.     public float minimumY = -45f;
    13.     public float maximumY = 45f;
    14.  
    15.     float rotationY = 0f;
    16.     float rotationX = 0f;
    17.  
    18.     // Update is called once per frame
    19.     void Update ()
    20.     {
    21.             rotationY += Input.GetAxis ("Mouse Y") * sensitivityY;
    22.             rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
    23.  
    24.             rotationX += Input.GetAxis ("Mouse X") * sensitivityX;
    25.             rotationX = Mathf.Clamp (rotationX, minimumX, maximumX);
    26.  
    27.             transform.localEulerAngles = new Vector3 (-rotationY, rotationX, 0);
    28.     }
    29. }
    But when i try to rotate the camera around the car, the camera just keeps spinning. I guess i am using the wrong input for "RotateAround" but i cant find any alternatives.
    This is my attempt:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class OrbitCamera3 : MonoBehaviour {
    5.  
    6.     public GameObject player;
    7.     public Transform center;
    8.  
    9.     public float sensitivityX = 5f;
    10.     public float sensitivityY = 5f;
    11.  
    12.     public float minimumX = -100f;
    13.     public float maximumX = 100f;
    14.  
    15.     public float minimumY = -45f;
    16.     public float maximumY = 45f;
    17.  
    18.     float rotationY = 0f;
    19.     float rotationX = 0f;
    20.  
    21.     // Use this for initialization
    22.     void Start () {
    23.         player = GameObject.FindWithTag ("Player");
    24.             center = player.transform;
    25.     }
    26.     // Update is called once per frame
    27.     void Update ()
    28.     {
    29.             rotationY += Input.GetAxis ("Mouse Y") * sensitivityY;
    30.             rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
    31.  
    32.             rotationX += Input.GetAxis ("Mouse X") * sensitivityX;
    33.             rotationX = Mathf.Clamp (rotationX, minimumX, maximumX);
    34.  
    35.         transform.RotateAround (center.position, Vector3.up, rotationX);
    36.         transform.RotateAround (center.position, Vector3.left, rotationY);
    37.         transform.LookAt (center);
    38.  
    39.  
    40.     }
    41. }
    Thanks for any help that might put my in the right direction ;)