Moving camera on a circle path

Discussion in 'iOS Development' started by friendlydev, Apr 6, 2009.

  1. friendlydev

    friendlydev

    New Member

    Joined:
    Dec 21, 2008
    Messages:
    153
    Hello,

    in my current iPhone project, I need to move the camera on a "circle path". Let me try to make this more clear. The camera needs to be focused on a certain object and I want to view this object from different angles. All these angles lie on an imaginary circle, which surrounds this object. You can easily compare it to the unity editor camera. If you select an object, you can rotate the camera around this object and view it from different angles. I want to do the exact same thing in my unity game.
    In theory, its a combination of rotating and moving the camera at the same time. The "only" problem I'm having right now, is to come up with a function, that moves and rotates the camera on a perfect circle path, so it always stays focused on my desired game object.

    Hope someone can point me into the right direction.
    Thanks in advance.
     
  2. friendlydev

    friendlydev

    New Member

    Joined:
    Dec 21, 2008
    Messages:
    153
    Getting closer:

    Just remembered something from school - thought I'd never need this "crap" again :)

    Maybe someone can tell me, if the following is correct:
    In theory, I need to define a center spot for my imaginary circle path. Then I need to use some math (there should be a formula) to calculate any spot on this circle, given a certain radius. This way I should be able to calculate every spot on the circle. Now, by moving the camera in a positive or negative direction on this path, it should rotate and move as expected, right?
     
  3. MikaMobile

    MikaMobile

    Member

    Joined:
    Jan 29, 2009
    Messages:
    812
    Why not have your camera parented to an empty game object that serves as your focal point, with the camera pointed directly at it. Rotating this focal point object would have the camera orbit around it in a circle.
     
  4. friendlydev

    friendlydev

    New Member

    Joined:
    Dec 21, 2008
    Messages:
    153
    Hehe :) That sounds a lot easier :) Thanks. I'll give it a try!
     
  5. friendlydev

    friendlydev

    New Member

    Joined:
    Dec 21, 2008
    Messages:
    153
    Darn...that was easy :) I'm affraid I'll never gonna be a good coder, if I can't stop thinking too complicated.
     
  6. MikaMobile

    MikaMobile

    Member

    Joined:
    Jan 29, 2009
    Messages:
    812
    Hehe, glad that worked out for you. Don't worry, it's easy to overthink things. Sometimes even the "pros" in this industry try to build a ferrari when a bicycle would suffice.
     
  7. bliprob

    bliprob

    Member

    Joined:
    May 13, 2007
    Messages:
    901
    I just did this a second ago, to debug some view angles:

    Code (csharp):
    1.  
    2. function Update () {
    3.     transform.RotateAround (Vector3.zero, Vector3.up, 30 * Time.deltaTime);
    4. }
    That will rotate the camera around the world origin automatically, at 30 degrees per second.

    If you want an interactive version, this script will rotate around a target (selected in the inspector) with pinching for zoom control. This is someone else's code (I mashed pinch-zoom and iphone-orbit code together, can't recall who should get the credit).

    Code (csharp):
    1. var distanceMin = 10.0;
    2. var distanceMax = 15.0;
    3. var distanceInitial = 12.5;
    4. var scrollSpeed = 1.0;
    5.  
    6. var xSpeed = 250.0;
    7. var ySpeed = 120.0;
    8.  
    9. var yMinLimit = -20;
    10. var yMaxLimit = 80;
    11.  
    12. private var x = 0.0;
    13. private var y = 0.0;
    14. private var distanceCurrent = 0.0;
    15.  
    16. @script AddComponentMenu ("Camera-Control/Key Mouse Orbit")
    17.  
    18. function Start () {
    19.     calibrateAccelerometer();
    20.    
    21.    var angles = transform.eulerAngles;
    22.     x = angles.y;
    23.     y = angles.x;
    24.  
    25.    distanceCurrent = distanceInitial;
    26.  
    27.    // Make the rigid body not change rotation
    28.       if (rigidbody)
    29.       rigidbody.freezeRotation = true;
    30. }
    31.  
    32. function LateUpdate () {
    33.     if (target) {
    34.         x -= getAccelerometer(iPhoneInput.acceleration).y * xSpeed * Time.deltaTime;
    35.         y += getAccelerometer(iPhoneInput.acceleration).x * ySpeed * Time.deltaTime;
    36.        distanceCurrent -= Input.GetAxis("Mouse ScrollWheel") * scrollSpeed;
    37.  
    38.     if (iPhoneInput.touchCount > 1) {
    39.         var touch : iPhoneTouch  = iPhoneInput.GetTouch(0);
    40.         var touch2 : iPhoneTouch = iPhoneInput.GetTouch(1);
    41.  
    42.         // Find out how the touches have moved relative to eachother:
    43.         var curDist: Vector2 = touch.position - touch2.position;
    44.         var prevDist : Vector2 = (touch.position - touch.positionDelta) - (touch2.position - touch2.positionDelta);
    45.  
    46.         distanceCurrent -= (curDist.magnitude - prevDist.magnitude)/2;
    47.     }
    48.      
    49.       distanceCurrent = Mathf.Clamp(distanceCurrent, distanceMin, distanceMax);
    50.        y = ClampAngle(y, yMinLimit, yMaxLimit);
    51.              
    52.         var rotation = Quaternion.Euler(y, x, 0);
    53.         var position = rotation * Vector3(0.0, 0.0, -distanceCurrent) + target.position;
    54.        
    55.         transform.rotation = rotation;
    56.         transform.position = position;
    57.     }
    58. }
    59.  
    60. var calibrationMatrix : Matrix4x4;
    61.  
    62. static function ClampAngle (angle : float, min : float, max : float) {
    63.    if (angle < -360)
    64.       angle += 360;
    65.    if (angle > 360)
    66.       angle -= 360;
    67.    return Mathf.Clamp (angle, min, max);
    68. }
    69.  
    70. function calibrateAccelerometer(){
    71.    var wantedDeadZone : Vector3  = iPhoneInput.acceleration;
    72.    var rotateQuaternion : Quaternion  = Quaternion.FromToRotation(new Vector3(0, 0, -1), wantedDeadZone);
    73.        
    74.    //create identity matrix ... rotate our matrix to match up with down vec
    75.    var matrix : Matrix4x4 = Matrix4x4.TRS(Vector3.zero, rotateQuaternion, new Vector3(1, 1, 1));
    76.  
    77.    //get the inverse of the matrix
    78.    this.calibrationMatrix = matrix.inverse;
    79. }
    80.    
    81. //Whenever you need an accelerator value from the user
    82. //call this function to get the 'calibrated' value
    83. function getAccelerometer(accelerator : Vector3 ) : Vector3 {
    84.    var accel : Vector3  = this.calibrationMatrix.MultiplyVector(accelerator);
    85.    return accel;
    86. }  
    87.  
     
  8. friendlydev

    friendlydev

    New Member

    Joined:
    Dec 21, 2008
    Messages:
    153
    Awsome! Thanks, very helpful indeed.
     
  9. Sunnee

    Sunnee

    New Member

    Joined:
    Apr 20, 2010
    Messages:
    22
    Hi guys

    This looks interesting to me as I am currently trying to do something similar but not as complex.

    Did this script work?

    I attached the script to a camera and I get this warning...

    ssets/CameraTarget.js(49,99): BCW0012: WARNING: 'UnityEngine.iPhoneTouch.positionDelta' is obsolete. "positionDelta property is deprecated. Please use iPhoneTouch.deltaPosition instead."
    [/code]
     
  10. serendip

    serendip

    New Member

    Joined:
    Feb 27, 2011
    Messages:
    6
    Not sure if anyone whats an 3.3 version...

    Code (csharp):
    1.  
    2. var distanceMin = 10.0;
    3. var distanceMax = 15.0;
    4. var distanceInitial = 12.5;
    5. var scrollSpeed = 1.0;
    6.  
    7. var xSpeed = 250.0;
    8. var ySpeed = 120.0;
    9.  
    10. var yMinLimit = -20;
    11. var yMaxLimit = 80;
    12.  
    13. private var x = 0.0;
    14. private var y = 0.0;
    15. private var distanceCurrent = 0.0;
    16.  
    17. @script AddComponentMenu ("Camera-Control/Key Mouse Orbit")
    18.  
    19. function Start () {
    20.     calibrateAccelerometer();
    21.    
    22.    var angles = transform.eulerAngles;
    23.     x = angles.y;
    24.     y = angles.x;
    25.  
    26.    distanceCurrent = distanceInitial;
    27.  
    28.    // Make the rigid body not change rotation
    29.       if (rigidbody)
    30.       rigidbody.freezeRotation = true;
    31. }
    32.  
    33. function LateUpdate () {
    34.     if (target) {
    35.         x -= getAccelerometer(Input.acceleration).y * xSpeed * Time.deltaTime;
    36.         y += getAccelerometer(Input.acceleration).x * ySpeed * Time.deltaTime;
    37.        distanceCurrent -= Input.GetAxis("Mouse ScrollWheel") * scrollSpeed;
    38.  
    39.     if (Input.touchCount > 1) {
    40.         var touch : Touch  = Input.GetTouch(0);
    41.         var touch2 : Touch = Input.GetTouch(1);
    42.  
    43.         // Find out how the touches have moved relative to eachother:
    44.         var curDist: Vector2 = touch.position - touch2.position;
    45.         var prevDist : Vector2 = (touch.position - touch.deltaPosition) - (touch2.position - touch2.deltaPosition);
    46.  
    47.         distanceCurrent -= (curDist.magnitude - prevDist.magnitude)/2;
    48.     }
    49.      
    50.       distanceCurrent = Mathf.Clamp(distanceCurrent, distanceMin, distanceMax);
    51.        y = ClampAngle(y, yMinLimit, yMaxLimit);
    52.              
    53.         var rotation = Quaternion.Euler(y, x, 0);
    54.         var position = rotation * Vector3(0.0, 0.0, -distanceCurrent) + target.position;
    55.        
    56.         transform.rotation = rotation;
    57.         transform.position = position;
    58.     }
    59. }
    60.  
    61. var calibrationMatrix : Matrix4x4;
    62.  
    63. static function ClampAngle (angle : float, min : float, max : float) {
    64.    if (angle < -360)
    65.       angle += 360;
    66.    if (angle > 360)
    67.       angle -= 360;
    68.    return Mathf.Clamp (angle, min, max);
    69. }
    70.  
    71. function calibrateAccelerometer(){
    72.    var wantedDeadZone : Vector3  = Input.acceleration;
    73.    var rotateQuaternion : Quaternion  = Quaternion.FromToRotation(new Vector3(0, 0, -1), wantedDeadZone);
    74.        
    75.    //create identity matrix ... rotate our matrix to match up with down vec
    76.    var matrix : Matrix4x4 = Matrix4x4.TRS(Vector3.zero, rotateQuaternion, new Vector3(1, 1, 1));
    77.  
    78.    //get the inverse of the matrix
    79.    this.calibrationMatrix = matrix.inverse;
    80. }
    81.    
    82. //Whenever you need an accelerator value from the user
    83. //call this function to get the 'calibrated' value
    84. function getAccelerometer(accelerator : Vector3 ) : Vector3 {
    85.    var accel : Vector3  = this.calibrationMatrix.MultiplyVector(accelerator);
    86.    return accel;
    87. }
    88.  
     
    Last edited: Mar 4, 2011