Hi guys,
I’m making a star shooter and I’ve got a problem with instantiating bullets. I’ve got four Game Objects tagged “Shoot” that specify bullets starting postition. Everything seems to be ok, but sometimes one of the bullets appears in the middle of the camera and doesn’t move like on the picture.
Here is the code:
using UnityEngine;
using System.Collections;
public class PlayerShooting : MonoBehaviour {
public GameObject bulletPrefab;
public float bulletSpeed = 400f;
public float delay = 0.1f;
private GameObject[] shootPoints;
private float shootTime;
private GameObject[] bullets;
// Use this for initialization
void Start () {
shootPoints = GameObject.FindGameObjectsWithTag("Shoot");
shootTime = Time.time;
}
// Update is called once per frame
void FixedUpdate()
{
if (Time.time > shootTime + delay)
{
if (Input.GetKey(KeyCode.Space))
{
foreach (GameObject shoot in shootPoints)
{
GameObject bullet = Instantiate(bulletPrefab, shoot.transform.position, shoot.transform.rotation) as GameObject;
Rigidbody2D rg2d = bullet.GetComponent<Rigidbody2D>();
rg2d.velocity = new Vector2(0f, bulletSpeed);
}
shootTime = Time.time;
}
}
}
}
I hope someone could suggest a solution.