Burst of Bullets?
Im trying to have my boss shoot a circular burst of bullets when its health equals 0.
using UnityEngine; using System.Collections;
public class BossBurst : MonoBehaviour {
public GameObject bossAttack;
public float bulletSpeed;
public Transform firePoint;
public GameObject bullet;
private float pinkBoBotAICurrentHealth;
// Use this for initialization
void Start ()
{
GameObject CurrentHealth = GameObject.Find ("CurrentHealth");
pinkBoBotAICurrentHealth = GameObject.Find ("The Pink Bobot").GetComponent<PinkBobotAI> ().CurrentHealth;
if (pinkBoBotAICurrentHealth >= 0) {
Instantiate (bossAttack, firePoint.position, firePoint.rotation);
GetComponent<Rigidbody2D> ().velocity = new Vector2 (0, bulletSpeed);
Instantiate (bossAttack, firePoint.position, firePoint.rotation);
GetComponent<Rigidbody2D> ().velocity = new Vector2 (-bulletSpeed, -bulletSpeed);
Instantiate (bossAttack, firePoint.position, firePoint.rotation);
GetComponent<Rigidbody2D> ().velocity = new Vector2 (-bulletSpeed, 0);
Instantiate (bossAttack, firePoint.position, firePoint.rotation);
GetComponent<Rigidbody2D> ().velocity = new Vector2 (-bulletSpeed, bulletSpeed);
Instantiate (bossAttack, firePoint.position, firePoint.rotation);
GetComponent<Rigidbody2D> ().velocity = new Vector2 (0, bulletSpeed);
Instantiate (bossAttack, firePoint.position, firePoint.rotation);
GetComponent<Rigidbody2D> ().velocity = new Vector2 (bulletSpeed, bulletSpeed);
Instantiate (bossAttack, firePoint.position, firePoint.rotation);
GetComponent<Rigidbody2D> ().velocity = new Vector2 (bulletSpeed, -bulletSpeed);
Instantiate (bossAttack, firePoint.position, firePoint.rotation);
GetComponent<Rigidbody2D> ().velocity = new Vector2 (bulletSpeed, 0);
}
}
// Update is called once per frame
void Update () {
}
}
Try to explain your problem completely ins$$anonymous$$d of just telling what a script is supposed to do and posting the script. Please be more specific next time.
Also, try to squeeze the repetitive part into a loop
Like for(int xCounter = -1; xCounter < 2; xCounter ++){ for (int yCounter = -1; yCounter < 2; yCounter ++){ GameObject currentAttack = (GameObject)Instantiate (bossAttack, firePoint.position, firePoint.rotation); currentAttack.GetComponent ().velocity = new Vector2 (bulletSpeed xCounter, bulletSpeed yCounter); } }
Answer by KidRigger · Apr 22, 2016 at 07:31 PM
And the problem is that it's not working. If the script is attached to the 'Enemy' then you are Getting the 'Rigidbody2D' component of the Game Object the script is attached to I.e. the 'Enemy'
Do this GameObject currentAttack = (GameObject)Instantiate (bossAttack, firePoint.position, firePoint.rotation) currentAttack.GetComponent<Rigidbody2D> ().velocity = new Vector2 (bulletSpeed, 0);
This way you'll get the component attached to the required GameObject.