Search Unity

My Camera script

Discussion in 'Scripting' started by zelrender, Jun 22, 2015.

  1. zelrender

    zelrender

    Joined:
    May 11, 2015
    Posts:
    47
    Hi all,

    Been using Unity for a couple of months now and feel comfortable with all the scripts I've written currently in my game. However, I've just gone back to the camera script I hobbled together and want to make some adjustments, but can't make total sense of what is there.

    The main adjustment I'd like to make is to change the angle of the camera.

    Anyway, here is my script, if anyone can advise, much appreciated.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MainCamera : MonoBehaviour {
    5.  
    6.     public GameObject Target; // The target in which to follow (player in this case)
    7.     public Vector3 CameraOffset = new Vector3(0,10,-20); // This will allow us to offset the camera for the player's view.
    8.     public float CameraSpeed = 10f; //How fast the camera will track the target
    9.  
    10.     private Camera _camera;
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.         _camera = GetComponent<Camera>();
    15.         }
    16.    
    17.     // Update is called once per frame
    18.     void FixedUpdate () {
    19.         // Check to see if the camera and the target exist
    20.         if (_camera !=null && Target != null) {
    21.  
    22.  
    23.             Vector3 targetPos = Target.transform.position;        //target coordinates
    24.             Vector3 offset = CameraOffset;
    25.  
    26.             float cameraAngle = _camera.transform.eulerAngles.y;        //camera angle on the y axis
    27.             float targetAngle = Target.transform.eulerAngles.y;            //target facing angle on the y axis
    28.  
    29.  
    30.                 targetAngle = cameraAngle;      
    31.    
    32.  
    33.  
    34.             // Adjust camera angle base on which way player is facing
    35.             targetAngle = Mathf.LerpAngle(cameraAngle, targetAngle, CameraSpeed * Time.deltaTime);        //LerpAngle is smooth
    36.             offset = Quaternion.Euler (0,targetAngle,0) * offset;
    37.    
    38.             _camera.transform.position = Vector3.Lerp (_camera.transform.position, targetPos + offset, CameraSpeed * Time.deltaTime);
    39.             //_camera.transform.LookAt (targetPos);        //rotates camera towards targetPos
    40.             }
    41.     }
    42. }
     
  2. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    I advice checking the API, so you understand that code before trying to change / modify something in it. Not that noone wants to help you, but we can only explain how to change things but not understand it for you, which should be your first goal you wanna reach :)

    As your code is documented in a good way, you could just try and error some things to see, how the script works.
     
  3. Limeoats

    Limeoats

    Joined:
    Aug 6, 2014
    Posts:
    104
    That camera script is a good starting point.

    I'm not sure what exactly you mean by change the angle of the camera. Do you want to make it look up/down? Left/right?

    You can accomplish all of this by changing the rotation of the camera's transform on a certain axis. Something like this...

    Code (CSharp):
    1. //Inside FixedUpdate
    2. if (Input.GetButton("N")) {
    3.     //Rotate the camera on the Y axis
    4.     //You can use transform.Rotate for this
    5. }
    I can't really test out code right now since I'm at work, but using transform.Rotate and passing it in the amount to rotate on the given axis will accomplish what you need.
     
  4. zelrender

    zelrender

    Joined:
    May 11, 2015
    Posts:
    47
    Thanks Limeoats. That pointed me in the right direction, however, the camera now rotates continuiously. I wanted to just change the static angle of the camera.
     
  5. zelrender

    zelrender

    Joined:
    May 11, 2015
    Posts:
    47
    Ok, I've been quite silly. I forgot that I can just change the static angle of the camera, or any gameobject for that matter, in the gameobject inspector.

    Was interesting to know how to change it dynamically though, so thanks.
     
  6. Limeoats

    Limeoats

    Joined:
    Aug 6, 2014
    Posts:
    104
    No problem, glad I can help!

    I'm actually in the process of implementing my dynamic camera controls, so if you have any questions, let me know!
     
    zelrender likes this.