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

How do you make an orbiting camera?

Discussion in 'Scripting' started by BarbaricSpaceWhale, Sep 8, 2015.

  1. BarbaricSpaceWhale

    BarbaricSpaceWhale

    Joined:
    Aug 26, 2015
    Posts:
    5
    Hello!

    I'm new to using the Unity engine, and I have a very basic understanding of C#. Right now, I'm focusing on being able to make very small games for the sake of practice, and I've decided to start with a simple 3D platformer.

    I'm having trouble figuring out how to make a camera that orbits around the player object. I want the camera to always be looking at the player, freely move on the X and Y axes, and have player movement be relative to the camera's angle. Think of Dark Souls or Super Mario Sunshine's camera systems.

    Any help, tips, or links to good guides would be appreciated.
     
  2. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    There's a lot of ways to do that. Probably the simplest way would be to make the camera a child of the player. So simply drag the camera onto the player gameObject in the Hierarchy, and offset it to the position and rotation you want it to be to be facing the player. When the player runs around, it'll automatically stay in it's local position relative to the player.

    Alternatively you could do it in code with something like this:

    Code (CSharp):
    1.     [SerializeField]
    2.     private GameObject player;
    3.     [SerializeField]
    4.     private Vector3 offsetPosition;
    5.  
    6.     void Update()
    7.     {
    8.         transform.position = player.transform.position - offsetPosition;
    9.         transform.LookAt(player.transform);
    10.     }
    Attach that to the camera, and in the inspector drag the player onto the 'player' variable in the inspector attached to the camera, and create the offset you want (for how far back and above you want him).
     
  3. BarbaricSpaceWhale

    BarbaricSpaceWhale

    Joined:
    Aug 26, 2015
    Posts:
    5
    Hello again.

    It turns out that what I really wanted out of this is a lot more complicated than I thought it would be at first and requires a lot of parts I don't know well yet.

    I actually wanted a control system with a camera that pivots around the player character and player movement based on camera direction, like an action adventure game, MMO or 3D platformer.

    Since that's more complicated than I predicted, I might just go with a simpler system with a single camera angle that follows a player that doesn't turn. In other words, that uses the camera system you gave here, so I want to say thanks for the code given. I might make another thread if I want to go over this again.
     
    Last edited: Sep 11, 2015
    Nigey likes this.