So I’m really new to unity and I’m trying to make a script that lets the player throw a ball which will disappear a few seconds after its been thrown so the scene doesn’t get cluttered but I can’t get it to work. I also want the player to have to wait for a few seconds after throwing five balls. I know this code is a mess but I’ve done the best I can and I’m very new to unity. Any help at all will be appreciated a lot and if anybody wants to point out the bits of code that I’m not using correctly I’d be grateful for that too. Basically thanks in advance for anything.
using UnityEngine;
usingsing System.Collections;
public class Throw : MonoBehaviour {
static public int BallsThrown = 0;
static public float Ammo = 5;
public float speed = 10.0f;
public Rigidbody Ball;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
GameObject drop = GameObject.FindWithTag("BallThrow");
if (Input.GetKeyDown (KeyCode.E))
{
Rigidbody newInstance = Instantiate (ball, drop.transform.position, transform.rotation) as Rigidbody;
Vector3 fwd = transform.TransformDirection (Vector3.forward);
newInstance.AddForce(fwd*speed, ForceMode.Impulse);
Destroy (rigidbody, 2.0f);
BallsThrown += 1;
if (BallsThrown >= Ammo) {
yield return new WaitForSeconds(5);
BallsThrown = 0;
}
}
}
}