Search Unity

Follow Orbit Camera

Discussion in 'Scripting' started by Redouane_, Sep 26, 2013.

  1. Redouane_

    Redouane_

    Joined:
    Jan 12, 2013
    Posts:
    3

    Hi! Can you guys help me with some tutorials or samples to start scripting my own Third person follow camera that orbit on X and Y of the mouse input.. i failed to do it with myself. :(

    Please help me!:)
     
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    There are standard ones that come with unity in the camera-controller package (I think its the camera controller package).


    On a side note, don't override default fonts in threads for no reason... especially with such a terrible one.
     
  3. Redouane_

    Redouane_

    Joined:
    Jan 12, 2013
    Posts:
    3
    Well. :p I know about standard ones that come with unity but i looking for tutorials about making your own camera script from scratch. Thanks for note
     
  4. WorldArchitect

    WorldArchitect

    Joined:
    Sep 27, 2013
    Posts:
    2
  5. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    357
    Try this.

    Code (csharp):
    1.  
    2.  
    3. public Transform target;
    4.  
    5. float xAxis;
    6. float yAxis;
    7.  
    8. const float distance = 10.0f;
    9. const float speed = 5.0f;
    10.  
    11. void Update {
    12.  
    13.     xAxis += Input.GetAxis("Mouse X") * speed;
    14.     yAxis  -= Input.GetAxis("Mouse Y") * speed;
    15.  
    16.     Quaternion rotation = Quaternion.Euler(yAxis, xAxis, 0.0f);
    17.  
    18.     transform.rotation = rotation; 
    19.  
    20.     transform.position = target.position + rotation * new Vector3(0.0f, 0.0f, -distance);
    21. }
    22.  
    23.  
     
    Last edited: Sep 29, 2013
    joelosiris91 and deazar like this.
  6. WorldArchitect

    WorldArchitect

    Joined:
    Sep 27, 2013
    Posts:
    2
    ^That doesn't work.

    This does:
    Code (csharp):
    1.     public GameObject target;
    2.     float radius = 3f, angleX = 0f, angleY = -45f;
    3.    
    4.     void Update()
    5.     {
    6.         radius -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * 100f;
    7.         angleX += Input.GetAxis("Mouse X") * Time.deltaTime;
    8.         angleY += Input.GetAxis("Mouse Y") * Time.deltaTime;
    9.  
    10.         float x = radius * Mathf.Cos(angleX) * Mathf.Sin(angleY);
    11.         float z = radius * Mathf.Sin(angleX) * Mathf.Sin(angleY);
    12.         float y = radius * Mathf.Cos(angleY);
    13.         transform.position = new Vector3(x + target.transform.position.x,
    14.                                          y + target.transform.position.y,
    15.                                          z + target.transform.position.z);
    16.         transform.LookAt(target.transform.position);
    17.     }
     
  7. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    357
    Look again.
     
  8. Redouane_

    Redouane_

    Joined:
    Jan 12, 2013
    Posts:
    3
    Thank's alot grizzly WorldArchitect both scripts work , this is exactly what i was looking for! i'll modify it based on my need :)
     
  9. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    357
    Enjoy :)