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. Dismiss Notice

Circle/Orbit pattern

Discussion in 'Scripting' started by Marscaleb, Aug 12, 2014.

  1. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    973
    I'm trying to make an object just constantly move in a circle when it is in its idle state.

    Technically, I have this much working, but it doesn't respond to the type of numbers I thought I would be using originally. I originally thought I would be setting a radius and speed, which translated into a sine wave, is its amplitude and frequency.

    But... this is the code I have right now:
    Code (CSharp):
    1. rigidbody2D.velocity = new Vector2( Mathf.Cos(Time.time * idleRadius) * idleSpeed, (Mathf.Sin(Time.time * idleRadius) * idleSpeed));
    And like this the IdleRadius actually makes an adjustment to how sharp of an angle the object is moving, and the speed isn't a frequency but just a raw speed, so changing the speed changes the radius.

    But I just don't know how else to make a circle using velocity, let alone how to make one that lets me adjust the circle's amplitude and frequency.
     
  2. Joxno

    Joxno

    Joined:
    Aug 20, 2013
    Posts:
    6
    Not currently sure how'd you go about doing it with a Rigidbody due to you having to supply a force inwards towards what you are orbiting as well as supplying a force outwards in order to create a stable orbit but a simpler way (if it suits your purpose) is to apply a position to the transform.position directly instead.

    And with a bit of maths..

    By passing an Angle into a Cosine function you will receive a coordinate on the X-Axis, and Sin will give you one on the Y-axis. These are points at specific angles in a 1 length circle.

    So by knowing such a point in a 1 length circle all you do to receive a specified radius is to multiply the point by your desired radius.

    So the desired solution would be:
    Code (CSharp):
    1.  
    2. Transform CenterPoint;
    3. var Radius = 3.0f;
    4. var Speed = 1.0f;
    5. this.transform.position = new vector2(CenterPoint.position.x + Mathf.Cos(Time.time*Speed)*Radius, CenterPoint.position.y + Mathf.Sin(Time.time*Speed)*Radius);
    6.  
     
  3. lrlelaldl

    lrlelaldl

    Joined:
    Jul 27, 2014
    Posts:
    75
    Wouldn't RigidBody(2d).MovePosition be better for this, as it teleports but also follows the laws of physics within the game? Would have a personal incline towards it for that reason
     
  4. Joxno

    Joxno

    Joined:
    Aug 20, 2013
    Posts:
    6
    Ah, haven't mucked about too much with RigidBody2D to notice it had such a function, but yes you are correct.

    So all in all, just switching out transform.position assignment for the MovePosition would be a lot better and should work as the OP wants it to.
     
  5. lrlelaldl

    lrlelaldl

    Joined:
    Jul 27, 2014
    Posts:
    75
    It also exists in the 3D RigidBody, useful when using your own physics but still want the collisions to be applied properly
     
  6. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    973
    Sweet merciful heaven, that's exactly the function I have needed for my moving platforms, but I never knew it existed!
     
  7. lrlelaldl

    lrlelaldl

    Joined:
    Jul 27, 2014
    Posts:
    75
    Would also make it easy to test for a collision with a wall or something if you are making it 'AI' based. Test whether it reached the destination it was supposed to reach, if it did all fine, if not reverse the direction