Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Moving camera on a circle path

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

  1. friendlydev

    friendlydev

    Joined:
    Dec 21, 2008
    Posts:
    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

    Joined:
    Dec 21, 2008
    Posts:
    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

    Joined:
    Jan 29, 2009
    Posts:
    845
    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.
     
    cb4nd17 and PrehistoricWiFi like this.
  4. friendlydev

    friendlydev

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

    friendlydev

    Joined:
    Dec 21, 2008
    Posts:
    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

    Joined:
    Jan 29, 2009
    Posts:
    845
    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

    Joined:
    May 13, 2007
    Posts:
    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 target : Transform;
    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(iPhoneInput.acceleration).y * xSpeed * Time.deltaTime;
    36.         y += getAccelerometer(iPhoneInput.acceleration).x * ySpeed * Time.deltaTime;
    37.        distanceCurrent -= Input.GetAxis("Mouse ScrollWheel") * scrollSpeed;
    38.  
    39.     if (iPhoneInput.touchCount > 1) {
    40.         var touch : iPhoneTouch  = iPhoneInput.GetTouch(0);
    41.         var touch2 : iPhoneTouch = iPhoneInput.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.positionDelta) - (touch2.position - touch2.positionDelta);
    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  = iPhoneInput.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.  
     
    MartinMa_ likes this.
  8. friendlydev

    friendlydev

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

    Sunnee

    Joined:
    Apr 20, 2010
    Posts:
    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

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

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