- Home /
My cannon that I am trying to make is spawning bricks from every direction and at 1 bullet per frame
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;
}
}
}
Answer by Konomira · Dec 11, 2016 at 11:40 PM
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 >
Answer by dragonking300 · Dec 12, 2016 at 12:22 AM
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?
Does it fire in the direction that it's facing? If you could provide an image of the bullet firing I'd be able to help.
It flys relative to a cube I have positioned. The way it calculates where to start placing the bullet is the magnitude of the 2 distances and what I imagined to happen was this
[ ] = cube for magnitude(cube has no collison) [ ___] = cannon O = bullet --- = pathing of the bullet
what I want to happen [ ] o --- [_]
what actually happens [ ] [___] o---- (bullet spawns from behind the cannon and goes up in a arc like this --- > ^
so basicly when I try to have my cannon shoot bullets it shoots the bullets in a arc up that goes up and then slides down to fly off the ledge
Do something like
float bulletSpeed = 5f;
bullet.transform.position = Vector3.$$anonymous$$oveTowards(cannon.transform.up, cube.transform.position,
bulletSpeed);
The cannon.transform.up should place it 1 unit above the cannon, you can multiply this value to fine tune the position of the bullet's spawn
Ok, now my cannon is firing bullets at 1 frame per second again. But it is firing bullets 1 frame per second after 5 seconds.
using UnityEngine;
using System.Collections;
public class CannonScript : $$anonymous$$onoBehaviour
{
float previousTime, waitTime;
public Transform Cannonballpos;
public GameObject Bullet;
public float ForceAmmount = 1000;
void Start()
{
previousTime = Time.timeSinceLevelLoad;
waitTime = 5;
}
int counter = 0;
void Update()
{
if (Time.timeSinceLevelLoad - previousTime > waitTime)
{
Fire();
previousTime = Time.timeSinceLevelLoad;
}
}
void Fire()
{
Rigidbody CannonRigidbody;
CannonRigidbody = Instantiate(Bullet, Cannonballpos.position, Cannonballpos.rotation) as Rigidbody;
CannonRigidbody.AddForce(Cannonballpos.forward * ForceAmmount);
counter = 0;
}
}
Here, I made a unity project and tested this out. Just drag the cube onto the target variable and the cannonball onto the projectile variable, the rest should work itself.
using UnityEngine;
public class CannonScript : $$anonymous$$onoBehaviour
{
float previousTime;
public GameObject
projectile, // The cannonball
target, // The cube that the cannon points at
cannonBallPos; // The spawn position of the cannon ball
public float
waitTime = 1, // Time between shots
forceAmount = 1000;
void Start()
{
previousTime = Time.timeSinceLevelLoad;
transform.localScale = new Vector3(1, 1, 2);
cannonBallPos = new GameObject("cannonBallPos"); // Creates a new game object
cannonBallPos.transform.parent = transform; // Sets the parent as the cannon
cannonBallPos.transform.position = transform.position + new Vector3(0, 0, 0.5f); // $$anonymous$$oves to the front of the cannon
}
void Update()
{
transform.LookAt(target.transform);
if (Time.timeSinceLevelLoad - previousTime > waitTime)
{
previousTime = Time.timeSinceLevelLoad;
Fire();
}
}
void Fire()
{
GameObject cannonBall;
cannonBall = (GameObject)Instantiate(projectile, cannonBallPos.transform.position, cannonBallPos.transform.rotation);
cannonBall.GetComponent<Rigidbody>().AddForce(transform.forward * forceAmount);
}
}
Sorry for such a late response but I have to saythank you so much for going out of your way and making the script. Imma use it as a learning tool and to make the cannons in my game
It's no problem, if you have any other questions feel free to ask.
Your answer
Follow this Question
Related Questions
How would I make a ListChangedEvent? 1 Answer
Unity 3D: How to make the gameobject speed increase continuously on tapping quickly? 1 Answer
Spawning 3D Objects according to Data at JSON Matrix 0 Answers
How to move the object to where the object is already pointing to? 1 Answer
"Follow Target" script with a prefab (C#) Help me please! 1 Answer