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

Resolved Diagonal sine wave movement

Discussion in '2D' started by pyc_01, Mar 10, 2023.

  1. pyc_01

    pyc_01

    Joined:
    Jan 5, 2023
    Posts:
    2
    I'm making a 2D game and want to spawn some enemies or obstacles who move in a diagonal sine motion. So far I have tried one parametric approach with a parameter t and a rotation matrix. The result isn't working as intended. The intended path is the image below:


    Code (CSharp):
    1. public class MoveSin_AngledTransform : MonoBehaviour
    2. {
    3.     float sinCenterY;
    4.     public float amplitude;
    5.     public float frequency;
    6.     private float angle; //rotation of the wave
    7.     private float t;
    8.    
    9.     void Start()
    10.     {
    11.         t = 0;
    12.         sinCenterY = transform.position.y;
    13.     }
    14.    
    15.     public void setAngle(float newAngle) {
    16.         angle = newAngle*(Mathf.PI/180);
    17.     }
    18.    
    19.     Vector3 transformcoords(float x_0, float alpha) {
    20.         float x = x_0;
    21.         float y = Mathf.Sin(x);
    22.        
    23.         float x_prime = x*Mathf.Cos(alpha) - y*Mathf.Sin(alpha);
    24.         float y_prime = x*Mathf.Sin(alpha) + y*Mathf.Cos(alpha);
    25.        
    26.         Debug.Log("Vector3(x_prime,y_prime,0)= " + x_prime+ ", "+y_prime);
    27.         return new Vector3(x_prime,y_prime,0);
    28.     }
    29.    
    30.     void FixedUpdate() {
    31.         Vector2 pos = transform.position;
    32.        
    33.         float sin = Mathf.Sin(pos.x * frequency) * amplitude;
    34.         pos.y = sinCenterY + sin;
    35.        
    36.         t+=Time.deltaTime*180*Mathf.PI; //parameter is an angle in polar coordinates
    37.         Vector3 transform_coords = transformcoords(t, angle);
    38.        
    39.         transform.position = transform_coords;
    40.     }
    41.  
    42.     }
    43. }
    The sine movement is referenced from this video. This was asked in another question but I am not trying to use an animation/controller like the answer suggests.

    How can I move the object as intended? Do I need to not use FixedUpdate()? Should I use Vector3.Lerp? Help is kindly appreciated.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    Mathf.Sin() will absolutely work 100% of the time and it cares NOTHING about FixedUpdate() or anything else.

    It takes a
    float
    and it gives a
    float
    .

    The input is in radians (not degrees) and it emits -1 to +1 values.

    Beyond that, to use Mathf.Sin your only options are:

    - change the rate of change of the input (eg, multiply by scale)
    - change the phase of the input (skew it forward / backwards in time without changing scale)
    - scale the output
    - offset the output
     
  3. pyc_01

    pyc_01

    Joined:
    Jan 5, 2023
    Posts:
    2
    Thanks for the reply.

    Am I correct in assuming my code doesn't work as intended because I am inputting polar coordinates and expecting to map the result to a Cartesian plane?

    In that case, it doesn't seem like I can get the diagonal sine result just by using a Lerp with the same transformation.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,521
    FYI: I removed the "2d physics" tag because your post is not discussing it.
     
  5. iMobCoding

    iMobCoding

    Joined:
    Feb 13, 2017
    Posts:
    160
    Can you go with different (and easier) approach where you attach that object to a parent which is rotated and moves in a linear path and the child just goes up/down localy with some easing curve?
     
    pyc_01 and Kurt-Dekker like this.
  6. After_Yesterday

    After_Yesterday

    Joined:
    Dec 29, 2019
    Posts:
    7