Search Unity

Flying in a circle

Discussion in 'Scripting' started by ICS499, Mar 3, 2010.

  1. ICS499

    ICS499

    Joined:
    Feb 20, 2010
    Posts:
    11
    Hello,

    I'm trying to make a dragon fly around in a circle at a set radius (ideally around the player).

    I've tried to use RotateAround like this:

    Code (csharp):
    1.  
    2. var degrees = 10;
    3. var circleMe : Transform;
    4.  
    5. function Update() {
    6.  
    7.     transform.RotateAround (circleMe.position, Vector3.up, degrees * Time.deltaTime);
    8.  
    9. }
    10.  
    This gets it to orbit around the player, but eventually the actual rotation of the dragon becomes off and it appears as if the dragon is flying backwards. Also the radius gets larger and larger as i move up and down on the terrain of my map.

    Is there a way to make it fly in a circle of a set radius, regardless of my players Y position?

    I would ideally want it to fly around while having its head (fixed) facing the player at all times.

    I'm not the best with Vectors and Math, so any help would be greatly appreciated.

    Thanks in advance!
    -Anthony
     
  2. Apostle77

    Apostle77

    Joined:
    Apr 25, 2009
    Posts:
    20
    You could use the LookAt() function to keep the dragon facing the player at all times.
     
  3. Amon

    Amon

    Joined:
    Oct 18, 2009
    Posts:
    1,384
    The problem is that you're increasing the transform values by deltatime which is why your dragon seems to fly off away from what it's supposed to be circling.

    I think anyway! :)

    Try having a seperate var for Vector.up and a value assigned to it that doesn't increase via delta time ut fo the tansform..
     
  4. Amon

    Amon

    Joined:
    Oct 18, 2009
    Posts:
    1,384
    Here! :)

    I modified the LookAt script to autmatically do what you want.

    Attach it to the object you want orbiting and for the target select the object to be orbted. :)

    Code (csharp):
    1.  
    2. var target : Transform;
    3. private var distance = 5.0;
    4.  
    5. private var xSpeed = 10;
    6.  
    7. private var x = 0.0;
    8. private var y = 0.0;
    9.  
    10. function Start () {
    11.     var angles = transform.eulerAngles;
    12.     x = angles.y;
    13.     y = angles.x;
    14.  
    15.     // Make the rigid body not change rotation
    16.     if (rigidbody)
    17.         rigidbody.freezeRotation = true;
    18. }
    19.  
    20. function FixedUpdate () {
    21.     if (target) {
    22.         x +=  xSpeed * Time.deltaTime;
    23.  
    24. var rotation = Quaternion.Euler(y, x, 0);
    25.         var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
    26.        
    27.         transform.rotation = rotation;
    28.         transform.position = position;
    29.     }
    30. }
    31.  
     
  5. RobbieDingo

    RobbieDingo

    Joined:
    Jun 2, 2008
    Posts:
    484
    You could of course do it the easy way?

    Make an object that a) follows your character and b) rotates on the spot. Then just make the dragonfly a child of this object but shift it's position over to the side a bit.

    Radius would be defined by the local position of the child, and LookAt() not needed...

    Just a thought.
     
  6. ICS499

    ICS499

    Joined:
    Feb 20, 2010
    Posts:
    11
    Thank you guys!