I'm new to unity and I'm experiencing something I am trying to make a gun animation making it go up and down but the problem is that after enough time the gun start moving from is original position.
Here is my code:
if (Input.GetMouseButton(0))
{
StartCoroutine(bouncing());
gun.GetComponent<Renderer>().material.color = Color.blue;
range = 80f;
}
if (Input.GetMouseButtonDown(1))
{
StartCoroutine(big_bouncing());
gun.GetComponent<Renderer>().material.color = Color.red;
range = 10f;
}
IEnumerator bouncing()
{
gun.transform.position = new Vector3(gun.transform.position.x, gun.transform.position.y + bounce, gun.transform.position.z);
yield return new WaitForSeconds(0.1f);
gun.transform.position = new Vector3(gun.transform.position.x, gun.transform.position.y - bounce, gun.transform.position.z);
}
IEnumerator big_bouncing()
{
gun.transform.position = new Vector3(gun.transform.position.x, gun.transform.position.y + big_bounce, gun.transform.position.z);
yield return new WaitForSeconds(0.1f);
gun.transform.position = new Vector3(gun.transform.position.x, gun.transform.position.y - big_bounce, gun.transform.position.z);
}
Start:
End:

