Unity Community


Results 1 to 10 of 10

  1. Posts
    153

    Moving camera on a circle path

    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. 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. Location
    Zombieville, USA
    Posts
    808
    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. Posts
    153
    Hehe That sounds a lot easier Thanks. I'll give it a try!


  5. 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. Location
    Zombieville, USA
    Posts
    808
    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. Location
    San Francisco, CA
    Posts
    901
    I just did this a second ago, to debug some view angles:

    Code:  
    1. function Update () {
    2.     transform.RotateAround (Vector3.zero, Vector3.up, 30 * Time.deltaTime);
    3. }
    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:  
    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. }


  8. Posts
    153
    Awsome! Thanks, very helpful indeed.


  9. 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. Location
    Calgary, Canada
    Posts
    6
    Not sure if anyone whats an 3.3 version...

    Code:  
    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(Input.acceleration).y * xSpeed * Time.deltaTime;
    35.         y += getAccelerometer(Input.acceleration).x * ySpeed * Time.deltaTime;
    36.        distanceCurrent -= Input.GetAxis("Mouse ScrollWheel") * scrollSpeed;
    37.  
    38.     if (Input.touchCount > 1) {
    39.         var touch : Touch  = Input.GetTouch(0);
    40.         var touch2 : Touch = Input.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.deltaPosition) - (touch2.position - touch2.deltaPosition);
    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  = Input.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. }
    Last edited by serendip; 03-04-2011 at 05:27 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •