Making an object bounce, using Mathf.Lerp

Hi there,

I’m trying to make an object (fireball) bounce between -4 and 10 on the Y Axis, without Rigidbody, and such. Instead, the object gets stuck on the exact same position.

void Update () {
	if (transform.position.y == -4) {
		transform.position = new Vector3(transform.position.x,Mathf.Lerp(min,max,Time.time),transform.position.z);
	}
	if (transform.position.y == 10) {
		transform.position = new Vector3(transform.position.x,Mathf.Lerp(max,min,Time.time),transform.position.z);
	}
}

I suspect the “==” causes the trouble, but “>” and “<” cause other problems.

The position.y hits unlikely exactly -4 or 10. Using Mathf.PingPong could be what you are after:

void Update () {
    transform.position = new Vector3(transform.position.x, Mathf.PingPong(Time.time * speed, 14) - 4, transform.position.z);
}

Another approach is using Mathf.Sin (returns -1 to 1) which gives an in/out easing.

void Update () {
    transform.position = new Vector3(transform.position.x, Mathf.Sin(Time.time * speed) * 7 + 3, transform.position.z);
}