- Home /
Bullet Spawn
Hi! Currently, I'm making an easy FPS game Here are codes I've written:
void Start () {
StartCoroutine (BulletRespawn());
}
// Update is called once per frame
void Update () {
}
IEnumerator BulletRespawn()
{
if(Input.GetMouseButton(0)) {
yield return new WaitForSeconds (0.1f);
Instantiate (bullet, respawnTr.position, Quaternion.identity);
}
}
And
public GameObject bullet;
public float bulletSpeed = 50.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
bullet.transform.Translate (0, -bulletSpeed * Time.deltaTime, 0);
}
And
public GameObject ChrCamera;
//public float mouseX;
//public float mouseZ;
public float chrSpeed = 10.0f;
// Use this for initialization
void Start () {
//Cursor.visible = false;
//Cursor.lockState = CursorLockMode.None;
}
// Update is called once per frame
void Update () {
//mouseX = Input.mousePosition.x;
//mouseZ = Input.mousePosition.z;
if (Input.GetKey (KeyCode.W)) {
ChrCamera.transform.Translate (0, 0, chrSpeed * Time.deltaTime);
}
if (Input.GetKey (KeyCode.S)) {
ChrCamera.transform.Translate (0, 0, -chrSpeed * Time.deltaTime);
}
if (Input.GetKey (KeyCode.A)) {
ChrCamera.transform.Translate (-chrSpeed * Time.deltaTime, 0, 0);
}
if (Input.GetKey (KeyCode.D)) {
ChrCamera.transform.Translate (chrSpeed * Time.deltaTime, 0, 0);
}
}
However, when I run the program, it crushes How shoud I change the code???
At the first script you dont have declaration of the bulllet
Second script you declared bullet but the update has transform translate which is meant be for bullet itself anyway I guess, you should look tutorial again for that.
Third script should work, disable others and see wasd moves camera arouns, like in fps games.
Answer by Grocho · Jun 30, 2018 at 08:46 AM
What's the error?
When I run the program, the unity itself stops and I can't even stop the program...
$$anonymous$$ake a Unity Crash Report.
That window pops up when Unity crashes, right?
Answer by fghfghfg · Jun 30, 2018 at 09:39 AM
After reading your code I highly recommend you watch a few tutorials on basic c# scripting within unity and watch a few on the particular subject of firing bullets.
Having said that... it would be nice to know what your error log is when you try to run this script.
I can see a few things that are probably not good ways to go about this sort of thing.. I would suggest that you get rid of the whole co-routine concept and opt of a much cleaner and more efficient route. First issue I see with your code is that there is no reference to the bullet you are trying to instantiate in your first script.. second thing is you should be checking for input down and input ups in an update method, this way on the input down you can start to InvokeRepeating() on a Shoot() method, inversely on input up you can cancel the invoke.. this is a much much more sensible way to do what you are trying to do.
Even if you do get this code working I highly recommend that you start studying as much as possible and when you get stuck with something look for a tutorial so you can see how other people achieve things, very powerful way to learn, if you try to do everything from scratch yourself you will still learn but you will form a lot of bad habits that will result in messy, buggy and un-reusable code.
I got what you're talking about. But I think you misunderstood what I've said. When I run the program, the Unity itself stops and I can't even stop running the program. Do you know how to solve this problem? Thank you
(p.s. I did made reference to the bullet but I think that I didn't copy it when posting it.)
Answer by lowhearted · Jun 30, 2018 at 05:39 PM
I am assuming the problem lies in your first script as you didn't handle the couroutine properly. I don't see why you'd need to use couroutines for this anyways. We can fix this and check if the input is received per frame. Try this:
void Update()
{
if (Input.GetButton("Fire");)
{
Instantiate(bullet, respawnTr.position, Quaternion.identity);
}
}
Also, you can get rid of the Start()
function. I also changed the input line as you were calling for a float and not a string. The Input.GetButton()
function takes a string as a parameter. I titled it for you, so you'd need to go to Edit > Project Settings > Input and create a field titled "Fire" to handle this.
I’ve used couroutines because I have to make delays between spawning bullets. Or else, I’ve spawn 60 bullets per second.