Search Unity

Sin movement on Y-Axis

Discussion in 'Scripting' started by sbuchbinder, Apr 23, 2008.

  1. sbuchbinder

    sbuchbinder

    Joined:
    Apr 4, 2008
    Posts:
    53
    I have my player flying through the scene correctly but would like to add a very minimal vertical Sin movement to the y-axis...so it looks like it's floating a little while flying (should go back and forth from moving up and down)

    Any help would be appreciated as I keep hitting a dead end.
     
  2. sbuchbinder

    sbuchbinder

    Joined:
    Apr 4, 2008
    Posts:
    53
    So basically I want to get rid of the counter and transforming the position by 0.1 all the time and replace it with an appropriate sin value.

    private var hCtr = 0;
    function Hover()
    {
    if(hCtr >= 10)
    {
    transform.position.y -= (transform.position.y * 0.1) * Time.deltaTime;
    if(hCtr >= 20)
    {
    hCtr = 0;
    }
    }
    else
    {
    transform.position.y += (transform.position.y * 0.1) * Time.deltaTime;
    }

    hCtr++;
    }
     
  3. biphenyl

    biphenyl

    Joined:
    Oct 16, 2007
    Posts:
    29
    try something like

    var amplitude:float;
    var frequency:float;

    transform.position += amplitude*(Mathf.Sin(2*Mathf.PI*frequency*Time.time) - Mathf.Sin(2*Mathf.PI*frequency*(Time.time - Time.deltaTime)))*transform.up;

    might be further optimizable.
     
  4. sbuchbinder

    sbuchbinder

    Joined:
    Apr 4, 2008
    Posts:
    53
    Matt...you rock!!

    This is exactly what I was looking for :)

    Mucho Thanks!!
     
  5. abi-kr01

    abi-kr01

    Joined:
    Aug 6, 2013
    Posts:
    4
    Thanks That helped
     
  6. chad18e

    chad18e

    Joined:
    Nov 30, 2019
    Posts:
    3
    It's 2022 and you still rock Matt! Thanks.