Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

any scripts for gameobject to move in circular motion ?

Discussion in 'Scripting' started by valentinodonny, Jul 7, 2009.

  1. valentinodonny

    valentinodonny

    Joined:
    Jul 7, 2009
    Posts:
    14
    hello, i am new to unity 3D. i was wondering if i can move my gameobject in a circular movement in my game scene ? can anyone help
     
  2. duck

    duck

    Unity Technologies

    Joined:
    Oct 21, 2008
    Posts:
    358
    There are lots of ways you could go about this. One way would be to move your object forward (along its Z axis), and rotate it a little (around its Y axis) each frame, using the following code in your Update() function:

    Code (csharp):
    1. transform.Translate(0,0,Time.deltaTime*0.1); // move forward
    2. transform.Rotate(0,Time.deltaTime*0.1,0); // turn a little
    in each place where deltaTime is multiplied by 0.1, you can change that value to affect the speed at which it moves forward or turns. Eg, *0.5 for Translate would cause it to move at 0.5 metres/sec.

    An altenative way would be to parent your object to an empty gameobject, then position it (in the editor) some distance away from the empty parent gameobject. You could then add some script to the empty gameobject to rotate it (similar to the 2nd of the two lines listed above). Because your object is a child of the empty gameobject, it will be rotated as if it were attached by a pivot to the empty parent object.

    hope this helps!

    - Ben
     
    JankTho and Ritt like this.
  3. valentinodonny

    valentinodonny

    Joined:
    Jul 7, 2009
    Posts:
    14
    thanks a lot!!
     
  4. HabibAli

    HabibAli

    Joined:
    Aug 19, 2013
    Posts:
    3
    How to use that code to move an object in an arc using an angle and centre point of the circle
     
    Ritt likes this.
  5. aitim

    aitim

    Joined:
    Jun 2, 2016
    Posts:
    2
    Thank you :)