how i am making a game where there is a ring which goes back and forth in the x direction and when i press a key i falls down now i want to instantiate another ring but it instantiates but clones the object 4 times i want it for one time
below is my code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RingController : MonoBehaviour {
Rigidbody rb;
public GameObject ring;
private float Ringtransition=0.0f;
private float Ringspeed = 2.5f;
private const float Boud_size = 6.5f;
// Use this for initialization
void Awake(){
rb = GetComponent<Rigidbody> ();
}
void Start () {
InvokeRepeating ("MoveRing", 0.019f, 0.019f);
}
void MoveRing(){
Ringtransition += Time.deltaTime * Ringspeed;
float Xposition = Mathf.Sin (Ringtransition) * Boud_size;
rb.transform.localPosition= new Vector3 (Xposition,6,0);
}
// Update is called once per frame
void Update ()
{
if (Input.anyKey) {
CancelInvoke ("MoveRing");
rb.useGravity = true;
Instantiate (ring, -ring.transform.localPosition,Quaternion.identity);
}
}
}