Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Camera Rotation Eludes me

Discussion in 'Scripting' started by BMRX, Feb 16, 2016.

  1. BMRX

    BMRX

    Joined:
    Nov 23, 2014
    Posts:
    51
    Bare with me, I'm not aware of any package that could solve this issue.

    Anyways, I have a camera that rotates around my player's X axis (hopefully X and Z at some point).
    This is achieved with a rightclick + drag. However the camera follows the rotation of the object when it also rotates. This is not what I want. How do I fix this?

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CameraController : MonoBehaviour
    5. {
    6.     public float rotationSpeed = 100.0F;
    7.     public float zoomSpeed = 100.0F;
    8.     private bool _isRotating;
    9.  
    10.     private GameObject playerShip;
    11.     private Vector3 shipPos;
    12.  
    13.     void Start()
    14.     {
    15.         playerShip = GameObject.Find("Ship");
    16.     }
    17.  
    18.  
    19.     void Update()
    20.     {
    21.         shipPos = playerShip.transform.position;
    22.         if (Input.GetMouseButtonDown(1))
    23.         {
    24.             rotating();
    25.         }
    26.  
    27.         if(Input.GetMouseButtonUp(1))
    28.         {
    29.             notRotating();
    30.         }
    31.        
    32.         if (_isRotating)
    33.         {
    34.             transform.RotateAround(shipPos, Vector3.up, Input.GetAxis("Mouse X") * rotationSpeed * Time.deltaTime);
    35.            
    36.  
    37.         }
    38.  
    39.         transform.Translate(Vector3.forward * Input.GetAxis("Mouse ScrollWheel") * zoomSpeed * Time.deltaTime);
    40.     }
    41.  
    42.     void rotating()
    43.     {
    44.         _isRotating = true;
    45.     }
    46.  
    47.     void notRotating()
    48.     {
    49.         _isRotating = false;
    50.     }
    51. }
     
  2. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    So you want the rotation to happen all the time? Just take away the condition that's restricting it to holding the mouse button. Remove all the "if" statements and whatever is in them, except this line.
    Code (CSharp):
    1. transform.RotateAround(shipPos, Vector3.up, Input.GetAxis("Mouse X") * rotationSpeed * Time.deltaTime);
     
    BMRX likes this.
  3. BMRX

    BMRX

    Joined:
    Nov 23, 2014
    Posts:
    51
    No that is not exactly what I was looking for, I should have been more specific. I don't want the camera to spin when the player object spins.

    I have now achieved it with this:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CameraController : MonoBehaviour
    5. {
    6.     public float rotationSpeed = 1.0F;
    7.     public float zoomSpeed = 100.0F;
    8.     private bool _isRotating;
    9.  
    10.     private GameObject playerShip;
    11.     private Vector3 offsetX;
    12.  
    13.     void Start()
    14.     {
    15.         playerShip = GameObject.Find("Ship");
    16.         offsetX = new Vector3(playerShip.transform.position.x, playerShip.transform.position.y + 1.5f, playerShip.transform.position.z - 6.0f);
    17.     }
    18.  
    19.  
    20.     void LateUpdate()
    21.     {
    22.         transform.LookAt(playerShip.transform.position);
    23.         if (Input.GetMouseButtonDown(1))
    24.         {
    25.             rotating();
    26.         }
    27.  
    28.         if(Input.GetMouseButtonUp(1))
    29.         {
    30.             notRotating();
    31.         }
    32.      
    33.         if (_isRotating)
    34.         {
    35.             offsetX = Quaternion.AngleAxis(Input.GetAxis("Mouse X") * rotationSpeed * Time.deltaTime, Vector3.up) * offsetX;
    36.             transform.position = playerShip.transform.position + offsetX;
    37.             transform.LookAt(playerShip.transform.position);
    38.         }
    39.         transform.position = playerShip.transform.position + offsetX;
    40.         transform.Translate(Vector3.forward * Input.GetAxis("Mouse ScrollWheel") * zoomSpeed * Time.deltaTime);
    41.     }
    42.  
    43.     void rotating()
    44.     {
    45.         _isRotating = true;
    46.     }
    47.  
    48.     void notRotating()
    49.     {
    50.         _isRotating = false;
    51.     }
    52. }
    53.  
    The issue is that the camera shakes violently, so I applied the smooth follow script. But now my zoom function does not work properly. I need to find a way to make the smooth follow adjust the distance based on what the zoom is set to.

    EDIT/ Zoom was kind of solved with; mainCamera.fieldOfView -= Input.GetAxis("Mouse ScrollWheel") * zoomSpeed;

    Now onto smoothing this out. It's very jerky, multiplying it by time.deltatime smooths it but makes it extremely slow. It's just as slow without multiplying it by zoom speed.

    EDIT// http://answers.unity3d.com/questions/519570/help-me-with-smooth-zoom.html Helped with zoom, I wish I figured this all out before posting.

    Thanks for stopping by.
     
    Last edited: Feb 16, 2016
  4. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    Code (CSharp):
    1. transform.position = Vector3.MoveTowards(transform.position, Vector3.forwatd * Input.GetAxis("Mouse ScrollWheel", zoomSpeed * Time.deltaTime)