- Home /
Classic Fireball Shooting Up Obstacle
I am trying to make a "fireball" (in this case water) spawn every 3 seconds, shoot straight up, and fall back down. Like the classic fireball obstacle in old Mario games.
With the code I have so far, it just sends the waterball straight up and it never comes back down. Also, another one does not spawn after 3 seconds. Coding is not my thing so if anyone could help that would be great.
using UnityEngine;
using System.Collections;
public class waterBall : MonoBehaviour {
public float shotDelay = 3f;
public Projectile projectile;
private float shotHeight = 20;
// Use this for initialization
void Start () {
if (shotDelay > 0) {
StartCoroutine(OnFire());
}
}
// Update is called once per frame
void Update () {
if (projectile) {
Projectile clone = Instantiate(projectile,transform.position, Quaternion.identity) as Projectile;
}
GetComponent<Rigidbody2D>().AddForce(new Vector2(0, shotHeight));
}
IEnumerator OnFire(){
yield return new WaitForSeconds(shotDelay);
StartCoroutine(OnFire());
}
}
Answer by Tomer-Barkan · Jul 04, 2015 at 05:22 AM
You check "projectile" before deciding whether to instantiate or not. I assume you want to create a new one when the first one is destroyed?
You add force to the projectile every frame (in the update). Instead just set the velocity to the starting velocity you want, but only once when instantiating, not every frame.
You need to add code to projectile that will destroy it after it finishes falling or the new one won't be created ever.
Make sure your projectile is affected by gravity.
Your answer
Follow this Question
Related Questions
How to Instantiate prefabs with function Update? 1 Answer
Delaying in for loop, Spawning enemies 1 Answer
delay first spawn 2 Answers
How do I put a delay in this script ? 1 Answer
WaitForSeconds Not Working 1 Answer