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

[Close this please]Getting the camera to follow target and rotate around target

Discussion in 'Scripting' started by ILemonDev, Mar 20, 2016.

  1. ILemonDev

    ILemonDev

    Joined:
    Dec 23, 2015
    Posts:
    11
    Hey guys. I have been trying to figure out how to do this for around 45 minutes. I am trying to get the camera to follow a target while allowing for the use of the Unity Engine function rotatearound().

    This is what i have got at the moment for moving the camera to rotate around:

    Code (CSharp):
    1. if (Input.GetKey(KeyCode.A))
    2. {
    3.      degrees += 60f * Time.deltaTime;
    4.      transform.RotateAround(target.position, Vector3.up, degrees);
    5.  }
    That does the rotation correctly. The problem i am having is when i use the below code to follow the target it will negate the rotatearound because i am setting the position to be changed. What can i use to let the camera do both of these things?

    Code (CSharp):
    1.  Vector3 targetCamPos = target.position + offset;  
    2. transform.position = Vector3.Lerp(transform.position, targetCamPos, smoothing * Time.deltaTime);
    Thanks
     
  2. ILemonDev

    ILemonDev

    Joined:
    Dec 23, 2015
    Posts:
    11
  3. ILemonDev

    ILemonDev

    Joined:
    Dec 23, 2015
    Posts:
    11
    So i have managed to get this which rotates the camera around the target around the y axis using the horizontal axis in input but i can't get it to move up and down. But i know that i need to use the Vertical Axis in input and put it in something similar to how horizontal works. I have tried to add offsetY to the transform.position but this doesn't give the effect of moving up and down. Any help please. Thanks

    Code (CSharp):
    1. `public Transform target;
    2.      public float smoothing = 5f;    
    3.      Vector3 offset;
    4.      Vector3 offsetY;
    5.      Vector3 difference;
    6.      // Use this for initialization
    7.      void Start ()
    8.      {
    9.          offset = transform.position - target.position;
    10.          offsetY = new Vector3(0, 0, 3);
    11.      }
    12.    
    13.      void LateUpdate()
    14.      {
    15.          offset = Quaternion.AngleAxis(Input.GetAxis("Horizontal") * smoothing, Vector3.up) * offset;
    16.          offsetY = Quaternion.AngleAxis(Input.GetAxis("Vertical") * smoothing, Vector3.right) * offsetY;
    17.          transform.position = target.position + offset;
    18.          transform.LookAt(target.position);
    19.      }
     
  4. ILemonDev

    ILemonDev

    Joined:
    Dec 23, 2015
    Posts:
    11
    Close this please. I have solved it.