Using mathf.sin
So I have everything worked perfectly with this
public float amplitude;
public float speed;
private float startPosition;
private Vector3 tempPos;
void Start ()
{
startPosition = transform.position.y;
}
void Update ()
{
tempPos.y = startPosition + amplitude * Mathf.Sin (speed * Time.time);
transform.position = tempPos;
transform.Rotate (0,0,300*Time.deltaTime);
}
the only problem is that the object always start in the center moving up and down. How do i make it so it stays where I position them?
Comment
Best Answer
Answer by IgorAherne · Apr 03, 2016 at 05:47 PM
void Update ()
{
tempPos = transform.position; //better place this in Start()
tempPos.y = startPosition + amplitude * Mathf.Sin (speed * Time.time);
transform.position = tempPos;
transform.Rotate (0,0,300*Time.deltaTime);
}
The issue was that tempPos
was (0, tempPos.y, 0)
always. Now that you make it equal to the transform.position, all xyz are set. So now tempPos
will have y changing, but x and z be the same as in the original transform.position
Your answer
![](https://koobas.hobune.stream/wayback/20220612074100im_/https://answers.unity.com/themes/thub/images/avi.jpg)