My cannon is a unity square stretched to be a rectangular prism and when I loaded my scene my cannon starts spawning bricks from every direction. To fix this I need to know 2 things. How to use coroutines to make my bullet prefab spawn every few seconds. How to make bullets shoot from the same consistent direction
using UnityEngine;
using System.Collections;
public class CannonScript : MonoBehaviour {
public GameObject Cannon;
public GameObject Bullet;
public float Bullet_Foward_Force;
void Start ()
{
}
int counter = 0;
void Update ()
{
counter++;
if (counter < 50000)
{
GameObject temporary_Bullet_handler = Instantiate(Bullet, Cannon.transform.position, Cannon.transform.rotation) as GameObject;
Rigidbody Temporary_Rigidbody;
Temporary_Rigidbody = temporary_Bullet_handler.GetComponent<Rigidbody>();
Temporary_Rigidbody.AddForce(transform.forward * Bullet_Foward_Force);
counter = 0;
}
}
}
This uses unity’s time counters to detect time differences between frames
float previousTime, waitTime;
void Start()
{
previousTime = Time.timeSinceLevelLoad;
waitTime = 5; // 5 seconds
}
void Update()
{
if (Time.timeSinceLevelLoad - previousTime > waitTime)
{
Fire();
previousTime = Time.timeSinceLevelLoad;
}
}
void Fire()
{
GameObject temporary_Bullet_handler = Instantiate(Bullet, Cannon.transform.position, Cannon.transform.rotation) as GameObject;
Rigidbody Temporary_Rigidbody;
Temporary_Rigidbody = temporary_Bullet_handler.GetComponent<Rigidbody>();
Temporary_Rigidbody.AddForce(transform.forward * Bullet_Foward_Force);
}
The method you used would work (I think), but the main problem I see is if (counter < 50000). It should be a >
thx, it fires as it should but it doesn’t fire is the direction I want it to no matter how I scale it. How do i fix that?