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

Orbit one object around another based on Input axis

Discussion in 'Scripting' started by Panchos, Aug 7, 2015.

  1. Panchos

    Panchos

    Joined:
    Oct 31, 2012
    Posts:
    34
    In my 2D game, I'm trying to make an objects position (in this case a camera target) sit somewhere in an 'orbit' around the player based on the direction of input.getaxis. The target must always be 1f distance away from the player.

    Eg. (when player is moving up-right)

    eg.gif

    Can anyone give me some pointers as to how this can be done? I'm not too brilliant at maths equations and such which is what I guess it'll need.

    EDIT: Some further info. I change my characters direction with 2 animation floats 'x_dir' and 'y_dir' which are pulled from input.getaxis on the controller. So I'm not able to just set a child object so it rotates with the player.
     
    Last edited: Aug 7, 2015
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    (in a script on the camera)
    Code (csharp):
    1. transform.RotateAround(player.transform.position, Vector3.up, rotateSpeed);
    where "player" refers to the player's GameObject or Transform and rotateSpeed is the amount it's rotating around in degrees (positive is clockwise, negative is counterclockwise). The exact distance will be maintained all on its own, unless you change it manually.

    EDIT: Nevermind, misread it. In this example though, if the player is moving up-right and the camera is up-right, does that mean you always want the camera facing the character from head-on?

    EDIT2: I think I see what you want. Just make an empty GameObject and make it a child of the player. Move it into the position you want (1f "ahead" of the player when he's facing forward) in the inspector, and it'll automatically rotate around to stay in that relative position whenever the character (the parent) is moving/rotating around. Make that empty GameObject the camera's target and you're set.
     
    Last edited: Aug 7, 2015
  3. Panchos

    Panchos

    Joined:
    Oct 31, 2012
    Posts:
    34
    Thanks. Regarding your Edit2 though, I tried changing my x position of the child camera target to 1 but it doesn't do what I want.

    Is it because my object doesn't actually rotate to turn in game? It's kinda pokemonesque rather than top view. I change anim floats and animation sprites to set direction
     
    Last edited: Aug 7, 2015
  4. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Edited. And yes ^_^.