Controlling the movement of an object using Mathf.Sin.

Hi,
I’m trying to get an object to bob up and down using Mathf.Sin -

public float speedUpDown = 1;
public float distanceUpDown = 1;

void Update ()
    {
         Vector3 mov = new Vector3 (transform.position.x, Mathf.Sin(speedUpDown * Time.time) * distanceUpDown, transform.position.z);
         transform.position = mov;
    }

This works but runs continuously. I need it to run for only one cycle, so I experimented with using it in a way that would let me stop and start it.
I’ve tried both if and while statements in the update and as a function outside of the update called from either update or start. I’ve also tried putting it into a coroutine.
The object doesn’t move. Is it that Mathf.Sin only works in update, or am I not calling it correctly?

Post the code which doesn’t work.

These are some of the things I tried. There were also many variations of these, but I would need sometime to remember them all.

public float speedUpDown = 1;
    public float distanceUpDown = 1;
    public bool nod;

void Update ()
    {
        while(nod)
        {
         Vector3 mov = new Vector3 (transform.position.x, Mathf.Sin(speedUpDown * Time.time) * distanceUpDown, transform.position.z);
         transform.position = mov;
        }
    }




    public float speedUpDown = 1;
    public float distanceUpDown = 1;
    public bool nod;

void Start ()
    {
         Vector3 mov = new Vector3 (transform.position.x, Mathf.Sin(speedUpDown * Time.time) * distanceUpDown, transform.position.z);
         transform.position = mov;
    }



    public float speedUpDown = 1;
    public float distanceUpDown = 1;
    public bool nod;

void  Start()
    {
        Nodding();
    }
        void Nodding()
        {
         Vector3 mov = new Vector3 (transform.position.x, Mathf.Sin(speedUpDown * Time.time) * distanceUpDown, transform.position.z);
         transform.position = mov;
        }
   


    public float speedUpDown = 1;
    public float distanceUpDown = 1;
    public bool nod;

void Update ()
    {
        Nodding();
    }

    void Nodding()
    {
        if(nod)
        {
         Vector3 mov = new Vector3 (transform.position.x, Mathf.Sin(speedUpDown * Time.time) * distanceUpDown, transform.position.z);
         transform.position = mov;
        }
    }

Whatever you do, please STOP writing code like this. Nobody can reason about code like this. Be kind to yourself.

How to break down hairy lines of code:

http://plbm.com/?p=248

Once it’s broken apart it’s only like 3 parts.

  1. establish a steadily increasing value for your input to Mathf.Sin, put it in a variable, output it with Debug.Log()

DON’T GO ANYWHERE UNTIL THE ABOVE WORKS!

  1. call Mathf.Sin() with the above value and print the resulting values out with Debug.Log(). Are they reasonable?

DON’T GO ANYWHERE UNTIL THE ABOVE WORKS!

  1. drive the transform with Vector3.up times the output

The above line of code does all 3 steps and fails. Please don’t code like that.

One thing per line please. Let your code practice social distancing.

1 Like

There is not a single coroutine here…

using System.Collections;
using UnityEngine;

public class MoveSin: MonoBehaviour
{
    [SerializeField] private float amplitude = 1f;
    [SerializeField] private float speed = 1f;

    private void Start() => StartCoroutine(SinMove());

    private IEnumerator SinMove()
    {
        var tau = 2 * Mathf.PI;
        for (var f = 0f; f < tau; f += speed * Time.deltaTime)
        {
            var v3 = transform.position;
            v3.y = Mathf.Sin(f) * amplitude;
            transform.position = v3;
            yield return null;
        }
    }
}
1 Like

Here are some of the attempts made using coroutines. The last one is not a coroutine but it did work.

public float speedUpDown = 1;
    public float distanceUpDown = 1;
    public bool nod;

void Start ()
    {
        StartCoroutine (Nodding());
    }

     IEnumerator Nodding()
    {
         Vector3 mov = new Vector3 (transform.position.x, Mathf.Sin(speedUpDown * Time.time) * distanceUpDown, transform.position.z);
         transform.position = mov;
        yield return null;
       
    }




    public float speedUpDown = 1;
    public float distanceUpDown = 1;
    public bool nod;

void Start ()
    {
        StartCoroutine (Nodding());
    }

     IEnumerator Nodding()
    {
        while(nod)
        {
         Vector3 mov = new Vector3 (transform.position.x, Mathf.Sin(speedUpDown * Time.time) * distanceUpDown, transform.position.z);
         transform.position = mov;
         yield return null;
        }
       
    }




    public float speedUpDown = 1;
    public float distanceUpDown = 1;
    public bool nod;

    void Update ()
    {
        StartCoroutine (Nodding());
    }

     IEnumerator Nodding()
    {
        while(nod)
        {
         Vector3 mov = new Vector3 (transform.position.x, Mathf.Sin(speedUpDown * Time.time) * distanceUpDown, transform.position.z);
         transform.position = mov;
         yield return null;
        }
       
    }

    public float speedUpDown = 1;
    public float distanceUpDown = 1;
    public bool nod;

void Update ()
    {
        StartCoroutine (Nodding());
    }

     IEnumerator Nodding()
    {
        if(nod)
        {
         Vector3 mov = new Vector3 (transform.position.x, Mathf.Sin(speedUpDown * Time.time) * distanceUpDown, transform.position.z);
         transform.position = mov;
         yield return null;
        }
    }





    public float speedUpDown = 1;
    public float distanceUpDown = 1;
    public bool nod;

void Start ()
    {
        StartCoroutine (Nodding());
    }

     IEnumerator Nodding()
    {
        if(nod)
        {
         Vector3 mov = new Vector3 (transform.position.x, Mathf.Sin(speedUpDown * Time.time) * distanceUpDown, transform.position.z);
         transform.position = mov;
        yield return null;
        }
    }




void Update ()
    {
        Nodding();
    }

     public void Nodding()
    {
         Vector3 mov = new Vector3 (transform.position.x, Mathf.Sin(speedUpDown * Time.time) * distanceUpDown, transform.position.z);
         transform.position = mov;
         yield return null;
    }