0
\$\begingroup\$
 // I'm using Unity2D =)

 // I'm trying to move the Transform.Position in a circle pattern and also move it forward.
 // I'm not trying to move transform.rotation at all just the transform.position.

 // This one moves in a circle pattern but unfortunately shakes violently which I don't want.
 // Also it does not move forward which I do want it to do.

 // Most importantly, I don't want it to immediately move on Start. I want it to move after something is true. When I put it on FixedUpdate it moved too quickly.

 // Thank you for any help! =)

     void Start()
     {
         StartCoroutine(MoveCircularly());
         StartCoroutine(MoveForward());
     }

     IEnumerator MoveForward()
     {
         while ( condition )
         {
             g.transform.Translate(Vector2.right);
             yield return new WaitForEndOfFrame();
         }
     }

     IEnumerator MoveCircularly()
     {
         while ( condition )
         {
             //move in a circle
             yield return new WaitForEndOfFrame();
         }
     }
\$\endgroup\$

3 Answers 3

1
\$\begingroup\$

It sounds like you're describing a curve called a helix:

Animation of an object moving along a helical trajectory (Animation via Gyre & Gimbal)

You can do this in a single function:

IEnumerator MoveInHelix(Matrix4x4 space, float radius, float frequency, float linearSpeed) {
    space = space * Matrix4x4.Scale(new Vector3(radius, radius, linearSpeed));

    float progress = 0;

    while(true) {
        progress += Time.deltaTime;
        float angle = progress * frequency * Mathf.PI * 2f;
        transform.position = space * new Vector3(Mathf.Cos(angle), Mathf.Sin(angle), progress);
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ Thank you for the response it looks reasonable, I was able to solve it but I can also try your code out. Much appreciated =) \$\endgroup\$
    – MM1Content
    Sep 7, 2019 at 1:07
1
\$\begingroup\$

what you are looking for is polar coordinates in 2d and variable z. maybe its called cylinder coordinates.

in polar or circular coordinates, position is defined by radius and angle. as unity and other simulation systems use cartesian coordinates, then you have to do a change coordinate to show it in unity.

 private void FixedUpdate()
    {
        foreach (var item in PolarElements)
        {

            float x = item.radius * Mathf.Cos(item.myAngle);
            float y = item.radius * Mathf.Sin(item.myAngle);

            item.position = new Vector3(transform.position.x+  x,transform.position.y+y,transform.position.z);
            item.myAngle += item.speed;
        }
    }

you can replace **transform.position.z** with any z value you want

\$\endgroup\$
1
  • \$\begingroup\$ Thank you for the response =). This one looks great for 2D which I needed and simpler to configure, I'll check this code out. Cheers! \$\endgroup\$
    – MM1Content
    Sep 7, 2019 at 16:58
0
\$\begingroup\$

I solved it by having one Game Object move in a circle and add a Game Object into the Game Object moving in a circle and have it move forward. That's the best I can do since IEnumerator wasn't working both functions at the same time for me.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .