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

Need some quick trig help, finding a point on a circle

Discussion in 'Scripting' started by Nanako, May 15, 2015.

  1. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    I'm trying to add some orbiting functions to my camera code, and i need a little bit of math help.

    The camera is at a fixed offset from the player, in terms of height and absolutedistance, those two variables will not change. So what i basically have is a circle, with the player/focal object in the centre, and my camera sitting at a point on the circumference of that circle.

    What i'd like to do therefore, is move the camera along the circumference, by a given distance. And to do that, i need to be able to calculate a point on the circumference a certain distance (or angle?) from my current one. I'm not quite sure where to start there.

    Here's a little pictoral example.


    Point a is where i'd like to move the camera to. How do i calculate that location? assuming i have the current locations of player and camera.
     
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    look at the standard asset camera controller, specifically orbit
     
  3. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    968
  4. HAlbera

    HAlbera

    Joined:
    Jun 7, 2013
    Posts:
    63
    Isnt there a rotatearound function?
     
    Nanako likes this.
  5. AlucardJay

    AlucardJay

    Joined:
    May 28, 2012
    Posts:
    328
  6. flaminghairball

    flaminghairball

    Joined:
    Jun 12, 2008
    Posts:
    868
    Haven't done this in a while, but:

    1. Take the difference from camera position to player position
    Code (csharp):
    1. Vector3 difference = player.position - camera.position;
    2. Construct a rotation quaternion from the angle you want to rotate by:
    Code (csharp):
    1. Quaternion rot = Quaternion.Euler(0, angle, 0);  //will rotate around the y axis by angle degrees
    3. Rotate your difference vector (multiplying a vector3 by a quaternion rotates it)
    Code (csharp):
    1. Vector3 rotatedCamPosition = rot * difference;
    4. Add the player position back in:
    Code (csharp):
    1. camera.position = player.position + rotatedCamPosition;
    Or, just camera.RotateAround(player.position, Vector3.up, angle);
     
  7. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    hii x thanks everyone, i was monitoring this thread while doing another project. I've eventually opted for the RotateAround function, which accomplishes this task nicely and with minimal fuss. That's the kind of thing i was looking for,i usually prefer to go with prebuilt native solutions over reinventing the wheel <3