Search Unity

Yaw and Rotation camera issue

Discussion in 'Scripting' started by Txguug, Oct 6, 2015.

  1. Txguug

    Txguug

    Joined:
    Jun 16, 2014
    Posts:
    18
    I slap this script to the camera which is a child object in an invisible sphere.
    It works really nicely as a free roaming camera script but there is one issue: using Q and E for rotation doesn't add up with the 'free look while holding middle mouse button' so it flips to a straight rotation when you click fire3
    I seem to be lacking the skill to figure this out... Everything that involves rotating things seems to paralyze my mind. Anybody who can help out?

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Camera : MonoBehaviour
    6. {
    7.     public float RotationSpeed = 60.0f;
    8.     public float KeySpeed = 7.0f;
    9.     public float ScrollSpeed = 50.0f;
    10.     public float MouseSensitivity = 4.0f;  
    11.     private float yaw = 0.0f;
    12.     private float pitch = 0.0f;
    13.     float minHeight = 200f;
    14.  
    15.     void Update()
    16.     {
    17.         Vector3 currentPosition = transform.parent.position;
    18.         float height = transform.parent.position.y;
    19.  
    20.         // WASD keyboard input
    21.         if (Input.GetKey(KeyCode.W))
    22.             transform.parent.Translate(Vector3.forward * Time.deltaTime * KeySpeed);
    23.         if (Input.GetKey(KeyCode.S))
    24.             transform.parent.Translate(Vector3.back * Time.deltaTime * KeySpeed);
    25.         if (Input.GetKey(KeyCode.A))
    26.             transform.parent.Translate(Vector3.left * Time.deltaTime * KeySpeed);
    27.         if (Input.GetKey(KeyCode.D))
    28.             transform.parent.Translate(Vector3.right * Time.deltaTime * KeySpeed);
    29.  
    30.         // QE rotation
    31.         if (Input.GetKey(KeyCode.Q))
    32.             transform.parent.RotateAround(currentPosition, Vector3.up, (-RotationSpeed * Time.deltaTime));
    33.         if (Input.GetKey(KeyCode.E))
    34.             transform.parent.RotateAround(currentPosition, Vector3.up, (RotationSpeed * Time.deltaTime));
    35.  
    36.         // scrollwheel to lower or raise camera
    37.         if (Input.GetAxis("Mouse ScrollWheel") < 0)
    38.             transform.parent.Translate(Vector3.up * Time.deltaTime * ScrollSpeed);
    39.         if (Input.GetAxis("Mouse ScrollWheel") > 0)
    40.             transform.parent.Translate(Vector3.down * Time.deltaTime * ScrollSpeed);
    41.  
    42.         // Free look while holding middle button
    43.         if (Input.GetButton("Fire3"))
    44.         {
    45.             yaw += MouseSensitivity * Input.GetAxis("Mouse X");
    46.             pitch -= MouseSensitivity * Input.GetAxis("Mouse Y");
    47.             transform.parent.eulerAngles = new Vector3(pitch, yaw, 0.0f);          
    48.         }
    49.  
    50.         if(height < minHeight)
    51.         {
    52.             transform.parent.position = new Vector3 (transform.parent.position.x, minHeight, transform.parent.position.z);
    53.         }
    54.  
    55.     }
    56. }
    57.  
     
  2. Txguug

    Txguug

    Joined:
    Jun 16, 2014
    Posts:
    18
    solved it, update transform.parent.eulerAngles.y when Fire3 is clicked:

    Code (CSharp):
    1.         // Free look while holding middle button
    2.         if (Input.GetButtonDown("Fire3"))
    3.             yaw = transform.parent.eulerAngles.y;
    4.         if (Input.GetButton("Fire3"))
    5.         {          
    6.             yaw += MouseSensitivity * Input.GetAxis("Mouse X");
    7.             pitch -= MouseSensitivity * Input.GetAxis("Mouse Y");
    8.             transform.parent.eulerAngles = new Vector3(pitch, yaw, 0.0f);
    9.             print(yaw);
    10.         }
    Thanks self! :)