Game object position is stuck when using mathf.sin,Object's position stuck when using Math.sin
Hello!
I am trying to making one of my objects bob up and down while at the same time moving left across the screen. The code I currently have achieves this, but now it keep snapping the object back to 0 on the y axis and begin bobbing and moving from there. If I manually move the object, or try to set the code to a different starting position, it will still throw it straight back to 0 on the y. Here is my code:
public float horizontalSpeed; public float verticalSpeed; public float amplitude;
private Vector3 tempPosition;
void Start ()
{
tempPosition = new Vector3 (4.32f, 2.28f, -0.66f);
//tempPosition = transform.position;
}
void FixedUpdate ()
{
tempPosition.x += horizontalSpeed;
tempPosition.y += Mathf.Sin(Time.realtimeSinceStartup * verticalSpeed)* amplitude;
transform.position = tempPosition;
}
I am new to Unity and coding so there is probably a simple solution to this but I cant seem to figure it out. I tried applying different methods from other peoples' questions but I keep running into the same problem.
Thanks in advance for your help!
Your answer
Follow this Question
Related Questions
Why does this script loop and glitch? 1 Answer
Lerp Problem 1 Answer
Make color pulse every 2 seconds 1 Answer