Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by wechat_os_Qy04hwWiqsL9Ov94DYr-DvD6Q · Jun 30, 2018 at 02:21 AM · instantiatefpscrashbulletrespawn

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???

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Shameness · Jun 30, 2018 at 05:10 AM 0
Share
  • 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.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Grocho · Jun 30, 2018 at 08:46 AM

What's the error?

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image wechat_os_Qy04hwWiqsL9Ov94DYr-DvD6Q · Jun 30, 2018 at 09:54 AM 0
Share

When I run the program, the unity itself stops and I can't even stop the program...

avatar image Grocho wechat_os_Qy04hwWiqsL9Ov94DYr-DvD6Q · Jun 30, 2018 at 10:59 AM 0
Share

$$anonymous$$ake a Unity Crash Report.

That window pops up when Unity crashes, right?

avatar image wechat_os_Qy04hwWiqsL9Ov94DYr-DvD6Q Grocho · Jun 30, 2018 at 12:13 PM 0
Share

Yeeeeeeees

Show more comments
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image wechat_os_Qy04hwWiqsL9Ov94DYr-DvD6Q · Jun 30, 2018 at 09:52 AM 0
Share

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.)

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image wechat_os_Qy04hwWiqsL9Ov94DYr-DvD6Q · Jul 01, 2018 at 02:06 AM 0
Share

I’ve used couroutines because I have to make delays between spawning bullets. Or else, I’ve spawn 60 bullets per second.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

140 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

FPS spawn bullet 0 Answers

rigidbody bullets dont spawn where i want them too. 1 Answer

Which is better, shooting a bullet from camera or from the gun itself? 3 Answers

Going crazy over simple bullets not instantiating in the correct position 0 Answers

Uneven firerate 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges