r/Unity3D•
thewolf20

Jumpy Movements when I combine Mathf.sin with time.deltatime

Question

Here's my code:

void Update ()
    {
	    if (!IsGrabbed)
        {
            timer += Time.deltaTime;

            gameObject.transform.Rotate(Vector3.up * 
Time.deltaTime * RotationSpeed);

            NewPositionY = (Mathf.Sin(timer * 
MoveSpeed) * MoveHeight) * Time.deltaTime ;
            transform.position = new 
Vector3(transform.position.x, NewPositionY + OffsetY, 
transform.position.z);
        }
    }

With this script I want to simuate a Up-Down floating movement while the object is rotating. But when I try to make the Up-Down movement framerate undependent (with time.deltatime; otherwise it moves very fast and high), it results in jittery/jumpy movements.

What is my problem in this code?

Posted May 13, 2017, 4:46 AM UTC

22
Loading comments…