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. Dismiss Notice

Rotate around vertically

Discussion in 'Scripting' started by MarkusDavey, Sep 11, 2011.

  1. MarkusDavey

    MarkusDavey

    Joined:
    Jun 3, 2011
    Posts:
    258
    Hello everyone,

    I'm sure this is a simple fix that I'm too blind to see, but, I have a camera than I want to be able to pitch up and down, whilst looking at an object.

    So, here is the code right now for the pitching.

    Code (csharp):
    1.  
    2. Vector3 axis = new Vector3(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"), 0);
    3.  
    4. if (axis.y != 0)
    5. {
    6.     transform.RotateAround(new Vector3(0, 1, 0), Vector3.right, axis.y * camSpeed);
    7. }
    8.  
    only issue is that it "sways" side to side and not up and down, no matter what I set. So, perhaps I'm just making a rookie mistake?

    Cheers.
     
  2. CrazySi

    CrazySi

    Joined:
    Jun 23, 2011
    Posts:
    538
    If I understood correctly you want to look at an object and be able to rotate around it on the camera Local X axis. If so then this works for me.

    Code (csharp):
    1. public class Look : MonoBehaviour {
    2.    
    3.     public GameObject targetObject;
    4.     public float camSpeed = 1;
    5.    
    6.     // Use this for initialization
    7.     void Start () {
    8.         transform.LookAt(targetObject.transform.position);
    9.     }
    10.    
    11.     // Update is called once per frame
    12.     void Update () {
    13.  
    14.         float mouseY = camSpeed * Input.GetAxis("Mouse Y");
    15.        
    16.         if (targetObject != null)
    17.         {              
    18.             Vector3 localX = transform.TransformDirection(Vector3.right);
    19.            
    20.             transform.RotateAround(targetObject.transform.position, localX, mouseY);
    21.         }
    22.        
    23.     }
    24. }
    25.  
     
    BinaryBanana likes this.
  3. MarkusDavey

    MarkusDavey

    Joined:
    Jun 3, 2011
    Posts:
    258
    This code gave back the exact same result, I wish for it to rotate vertically, and yet this code causes it to orbit elliptically up and down. Any idea why that is?
     
  4. CrazySi

    CrazySi

    Joined:
    Jun 23, 2011
    Posts:
    538
    I'm a bit unclear on what you want. Because what I gave you will allow you to rotate the camera around a given object whilst looking at it. You said you want an camera that can "pitch up and down, whilst looking at an object". Do you mean you want a camera that elevates up in a straight line looking at the target but does not rotate about it?

    Like this?

    Code (csharp):
    1. public class Look : MonoBehaviour {
    2.    
    3.     public GameObject targetObject;
    4.     public float camSpeed = 15;
    5.    
    6.     // Use this for initialization
    7.     void Start () {
    8.         transform.LookAt(targetObject.transform.position);
    9.     }
    10.    
    11.     // Update is called once per frame
    12.     void Update () {
    13.  
    14.         float mouseY = camSpeed * Input.GetAxis("Mouse Y");
    15.        
    16.         if (targetObject != null)
    17.         {              
    18.             transform.Translate(new Vector3(0, mouseY, 0), Space.World);
    19.             transform.LookAt(targetObject.transform.position);                  
    20.         }
    21.        
    22.     }
    23. }
     
  5. BinaryBanana

    BinaryBanana

    Joined:
    Mar 17, 2014
    Posts:
    81
    Thanks! Works great.
     
  6. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,589
    Please use the like button instead of necroing a 12 year old thread :D