Search Unity

Camera that can follow an orbiting player

Discussion in 'Scripting' started by DJ_JW, Nov 6, 2017.

  1. DJ_JW

    DJ_JW

    Joined:
    Apr 26, 2015
    Posts:
    24
    Hi team

    I tried to ask this on Unity Answers but cannot as the captcha is not working (error message where catpcha should be)

    Essentially I am trying to find a way to create camera that is a combination of a smooth follow camera and a orbit camera

    I have a player that orbits around a pole (think cylinder that is fixed in position) in the centre of my scene. The player is able to move up and down the pole as it orbits around at a "semi" fixed distance from the pole.
    I can get the player to orbit (move) around the pole and not move too far from the center of the pole and move up and down the pole but I cannot find a way to have the camera follow the player around the pole (but always face the pole) - think like a standard orbit camera but instead of using the mouse or orbit around, the camera follows the player.

    I am figuring their will be some magic maths to make this work but for the life of me cannot get my head around how to do it.

    I would just have the camera follow the player transform and "look" towards the pole, my biggest issue with this is that the player can be facing any arbitrary direction at any time (physics player controller) and this causes the camera to face in odd directions as the player changes direction.

    Can anyone point me in the best direction to achieve this, or has someone already created a camera controller to do this that they would be willing to share?

    Thanks in advance.
     
  2. Enyss

    Enyss

    Joined:
    Oct 4, 2016
    Posts:
    7
    How about something like this ?

    Code (csharp):
    1.  
    2.  
    3. public class OrbitingCamera : MonoBehaviour {
    4.  
    5.     Vector3 poleCenter; // Your pole center
    6.     Transform player;  // Your player Transform
    7.  
    8.     [SerializeField]
    9.     float distance;  // The distance between your camera and the player
    10.  
    11.    // Update is called once per frame
    12.    void Update ()
    13.     {
    14. Vector3 v = new Vector3(poleCenter.x, player.position.y, poleCenter.z);
    15.         Vector3 direction = Vector3.Normalize(player.position - v);
    16.         transform.position = player.position + distance * direction;
    17.         transform.LookAt(player, Vector3.up);
    18.    }
    19. }
    20.  
     
  3. DJ_JW

    DJ_JW

    Joined:
    Apr 26, 2015
    Posts:
    24
    OMG @Enyss that looks way more simple to what I was trying to achieve.
    Thank you very much for the super fast response to my post.

    I will give this a go, I thought [transform.LookAt(player, Vector3.up)] caused the camera to face odd directions based on Player orientation, though I could well have been misusing it.

    I'll let you know as soon as I have tested.
     
  4. DJ_JW

    DJ_JW

    Joined:
    Apr 26, 2015
    Posts:
    24
    @Enyss , thanks for taking the time to respond, this worked perfectly.