Search Unity

How to achieve (circular-spiral) movement

Discussion in '2D' started by amjadyahya1, Apr 3, 2020.

  1. amjadyahya1

    amjadyahya1

    Joined:
    Aug 5, 2016
    Posts:
    12
    I want to achieve the following unusual curve movement (illustrated in the image below)

    I don't know how this kind of movement is called which makes it hard to search for a formula.
    Appreciate any input. Thanks.
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    You can make that kind of movement by first moving an object in a circle, then adding a positive X offset over time.

    You could try using transforms to do this if you don't want to work out the math.

    Have a parent transform, and a child object offset from the local center. Rotate the parent while also moving it, and the child will move in that looping pattern. Changing the rotation rate and the movement rate will affect the size and frequency of loops.
     
  3. amjadyahya1

    amjadyahya1

    Joined:
    Aug 5, 2016
    Posts:
    12
    Thanks.

    But there must be a math equation that can describe such a movement.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,456
    https://gyazo.com/54e29b30f165190304c973fc0ffd19ff
    https://gyazo.com/dc4260dbf5e16f123b1ae2b0df575c84


    Here's one way of doing it. I added this script and a TrailRenderer to the GameObject.

    Code (CSharp):
    1.  
    2. using System;
    3. using UnityEngine;
    4.  
    5. public class SpiralMovement : MonoBehaviour
    6. {
    7.     // Controls the radius of the circular motion for X/Y axis.
    8.     public Vector2 Radii = new Vector2(2f, 3f);
    9.    
    10.     // Controls the direction/speed the spiral translates
    11.     public Vector2 LinearVelocity = new Vector2(0.5f, 0f);
    12.    
    13.     // Controls the rotational speed of the spiral.
    14.     public float AngularVelocity = 1f;
    15.    
    16.     // Scales the speed of the spiral motion,
    17.     public float TimeScale = 2f;
    18.  
    19.     float m_Time;
    20.  
    21.     void Update()
    22.     {
    23.         // Calculate the angle at this time.
    24.         var angle = AngularVelocity * m_Time;
    25.        
    26.         // Calculate the linear translation at this time.
    27.         var position = LinearVelocity * m_Time;
    28.        
    29.         // Add in the circular motion.
    30.         position += new Vector2(Mathf.Cos(angle) * Radii.x, Mathf.Sin(angle) * Radii.y);
    31.        
    32.         // Set the transform to this spiral position.
    33.         transform.position = position;
    34.  
    35.         // Adjust time.
    36.         m_Time += TimeScale * Time.deltaTime;
    37.     }
    38. }
    39.  
     
    Last edited: Apr 4, 2020
    Extiward and LiterallyJeff like this.
  5. amjadyahya1

    amjadyahya1

    Joined:
    Aug 5, 2016
    Posts:
    12
    Thanks, that's what I was looking for.
     
    MelvMay likes this.