Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Third Person Camera Help Please

Discussion in 'Scripting' started by TheFreedomBird, Dec 20, 2014.

  1. TheFreedomBird

    TheFreedomBird

    Joined:
    Dec 19, 2014
    Posts:
    8
    So, I'm new to game design and scripting. I recently started working on a game and wanted a third person camera. I then found a few scripts and applied them to my camera. The only problem is I cant look up or down I'm just fixed to rotating in y-axis and can only change the horizontal view. The scripting language is in C# and I'm using a mouse aim camera with a dungeon camera. Can someone help me figure out how to look up and down without changing languages.
    This is the Mouse Aim script:

    Code (CSharp):
    1. public class MouseAimController : MonoBehaviour {
    2. public GameObject target;
    3. public float rotateSpeed = 5;
    4. Vector3 offset;
    5.  
    6. void Start() {
    7. offset = target.transform.position - transform.position;
    8. }
    9.  
    10. void LateUpdate() {
    11. float horizontal = Input.GetAxis("Mouse X") * rotateSpeed;
    12. target.transform.Rotate(0, horizontal, 0);
    13.  
    14. float desiredAngle = target.transform.eulerAngles.y;
    15. Quaternion rotation =Quaternion.Euler(0, desiredAngle, 0);
    16. transform.position = target.transform.position - (rotation * offset);
    17.  
    18. transform.LookAt(target.transform);
    19. }
    20. }
    This is the Dungeon Crawler script:

    Code (CSharp):
    1. public class DungeonCrawlerCamera : MonoBehaviour {
    2.  
    3.     public GameObject target;
    4.     public float damping = 1;
    5.     Vector3 offset;
    6.  
    7.     void Start () {
    8.  
    9.         offset = transform.position - target.transform.position;
    10.     }
    11.  
    12.     void LateUpdate () {
    13.  
    14.         Vector3 desiredPostion = target.transform.position + offset;
    15.         transform.position = desiredPostion;
    16.         Vector3 position = Vector3.Lerp (transform.position, desiredPostion, Time.deltaTime * damping);
    17.         transform.position = position;
    18.  
    19.         transform.LookAt (target.transform.position);
    20.     }
    21. }
     
    Last edited: Dec 20, 2014
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    I think you would do this:
    Code (csharp):
    1.  
    2. void LateUpdate() {
    3. float horizontal = Input.GetAxis("Mouse X") * rotateSpeed;
    4. float vert = Input.GetAxis("Mouse Y") * rotateSpeed;
    5. target.transform.Rotate(vert, horizontal, 0);
    6.  
     
  3. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    Code (CSharp):
    1. public class MouseAimController : MonoBehaviour {
    2. public GameObject target;
    3. public float rotateSpeed = 5;
    4. Vector3 offset;
    5.  
    6. void Start() {
    7. offset = target.transform.position - transform.position;
    8. }
    9.  
    10. void LateUpdate() {
    11. float horizontal = Input.GetAxis("Mouse X") * rotateSpeed;
    12. target.transform.Rotate(0, horizontal, 0);
    13.  
    14. float desiredAngle = target.transform.eulerAngles.y;
    15. Quaternion rotation =Quaternion.Euler(0, desiredAngle, 0);
    16. transform.position = target.transform.position - (rotation * offset);
    17.  
    18. transform.LookAt(target.transform);
    19. }
    20. }
    It won't use the y axis because nowhere have you told it to.
     
  4. TheFreedomBird

    TheFreedomBird

    Joined:
    Dec 19, 2014
    Posts:
    8
    So then is there a way to tell it to use the y axis. Also I did try the line that fire7side recommended and it did nothing so can I tell it to use the y-axis.
     
  5. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    I'm kinda rubbish at understanding C# but I'm sure this little bit of code might come in useful :)

    Code (JavaScript):
    1. float Vertical = Input.GetAxis("Mouse Y") * rotateSpeed;
    then you would add the code so it rotates on the Y to be honest though look at the cam that comes with Unity Standard Assets I learnt loads about Cams from looking at the coding It isn't that hard to convert it to C# as the files that come are JS. Hope this helps you out anyway.
     
  6. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    It should use the y axis right here:
    1. target.transform.Rotate(vert, horizontal, 0);
    Are you sure you used all the code I posted? That line was changed from the original to add the second variable, vert.
     
    Last edited: Dec 20, 2014
  7. TheFreedomBird

    TheFreedomBird

    Joined:
    Dec 19, 2014
    Posts:
    8
    Ya I used all the script and it didn't do anything. I had already tried what Josenifftod had put, that was my first instinct but it said I couldn't use Vertical.