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

[Solved] Using Mathf.Sin to create a looping Up and down mouvement

Discussion in 'Scripting' started by krousty_bat, Feb 28, 2016.

  1. krousty_bat

    krousty_bat

    Joined:
    Oct 9, 2012
    Posts:
    60
    Hello

    I would like to scroll a 2D object on Y (up and down) for ever.
    I thought of Using a Sin.x, with x being the time value (Time.time or Time.deltaTime)

    Code (CSharp):
    1. BgYPosition = Mathf.Sin (Time.time*upAndDownSpeedMultiplier);
    2. Vector3 scrollY = new Vector3 (0f,BgYPosition,0f);
    3. transform.position= scrollX + scrollY;;  //scrollX being a simple scroll on X not written here
    4.  
    the question is, how do I set this up so the Amplitude (y) of my Sin.curve (so the max + min height of my curve) to be independent from the speed of my sin.x (driven by Time.time*Speed I guess, giving the speed it's going up and down)




    Thx a lot.
     
    Last edited: Feb 28, 2016
  2. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    You're problem is that when you change the oscillation speed, your 2D object position is jerky.
    Is that right?
     
  3. krousty_bat

    krousty_bat

    Joined:
    Oct 9, 2012
    Posts:
    60
    Ok, found it by myself :)...
    (wich is good :)

    Thx.
     
  4. krousty_bat

    krousty_bat

    Joined:
    Oct 9, 2012
    Posts:
    60
    Thx Eric
    no the problem was that I was trying to guess how to change and setup the speed of my "up and down movement" (my phase?) and of my y (amplitude) separetely.

    I didi it by writing something like
    Code (CSharp):
    1.             BgYPosition = 1.75f*Mathf.Sin (Time.time/3) - 2;
    were 1.75f is my y, 3 is the speed of my up and down, and -2 is were my middle curve will start on my screen :)
     
  5. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    The formula for that is:

    y = a*sin(2*pi*f*t )

    Where:
    a is the amplitute
    f is the number of oscillation per second
     
    nhanc18 and krousty_bat like this.