Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by caleb_b · Sep 30, 2014 at 10:57 PM · c#prefabdestroyshooting

Minor bugs in my gun shooting script

I've created this script attached to a gun. The script fires the gun, and I am working to make the gun deal damage if the bullet hits an enemy. Here is the script

 using UnityEngine;
 using System.Collections;
 
 public class PlayerGunController : MonoBehaviour 
 {
     public float fireRate = 1;
     public GameObject bullet;
     public int damage;
     public int ammo = 15;
 
     private float nextFire = 1;
 
     void Update () 
     {
         if(Input.GetButton("Fire1") && Time.time > nextFire)
         {
             nextFire = Time.time + fireRate;
             Fire();
             ammo--;
         }
     }
     void Fire() 
     {
         float range= Mathf.Infinity;
         Ray ray= Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;
 
         if(Physics.Raycast(ray, out hit, range))
         {
             bullet.transform.position = hit.point;
             if(hit.transform.gameObject.tag == "Enemy")
             {
                 hit.transform.GetComponent<EnemyHealth>().health -= damage;
             }
         }
     }
 }

The functionality of this script is simple. There is a block in the level that acts as the bullet. It is assigned as Bullet for this script. When the "fire" button is clicked, the block's position is changed to the location of the cursor at that moment. If the object the block is touching is tagged as enemy, the enemy takes damage. It works in that sense.

The problem is that is "fire" is pressed multiple times without moving the cursor, the first block hits the enemy, but the second click only hits the position of the previous block, not the enemy.

I was thinking I could just say to destroy the gameobject when it hits something (Don't know how or where I'll do that. Haven't gotten that far yet). The problem with that is there is only one bullet. If I destroy it, that's it. No more ammo. We can't have that.

The solution for THAT problem is to use prefabs. So can I edit this script to retain this functionality, but use a prefab of a bullet instead?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Habitablaba · Sep 30, 2014 at 11:05 PM

You're not really using that 'bullet' the way you think you are.

What is likely happening here is:

  • Ray cast from mouse pointer forward

  • Ray hits object, returns true

  • Move 'bullet' to collision point

  • check if the raycast hit an enemy, returns true

  • do damage

  • Ray cast again, hitting the bullet that you just moved, returns true (it did hit something after all)

  • check if the raycast hit an enemy, return false (it hit the bullet)

Instead of keeping an actual bullet around that you transform from place to place, why not just represent "ammo" as a number? you are already doing ammo--; so it isn't that far off from what you're doing. As a bonus, you don't have to worry about prefabs, and the game won't slow down if you fire your gun a whole bunch of times.

Comment
Add comment · Show 3 · 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 caleb_b · Sep 30, 2014 at 11:20 PM 0
Share

Not entirely sure what you mean with the whole "ammo as a number thing." But you explanation of the actual process helped. I don't need a bullet object at all. It's the Raycast that matters. So I just took the whole deal about transfor$$anonymous$$g the position of the bullet out, and eli$$anonymous$$ated the bullet altogether. Now the ray casts out, hits an object, and checks the tag. If the tag is an enemy, health is taken away from it. End of story. Thanks Habit!

avatar image Habitablaba · Sep 30, 2014 at 11:24 PM 1
Share

What I mean by ammo as a number is that you just have some number that keeps track of how much ammo your gun has.

 int ammo = 15; // You do this already.

Then every time you shoot, you subtract from your ammo.

 ammo--; // You are doing this already.

Then you just check to see if you have ammo before you shoot.

 Input.GetButton("Fire1") && Time.time > nextFire && ammo > 0

So really, you're already doing it, except for the part where you check to see if they have ammo before they fire the gun.

avatar image caleb_b · Oct 01, 2014 at 12:18 AM 1
Share

Oh! I get it! Sorry, I thought I had that function in there Lol I meant to but I forgot it. Thanks again!

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

27 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

Related Questions

Destorying prefab and instanstiating again? 0 Answers

How do I destroy a Instantiated UI image that is pushed on the Canvas? 2 Answers

Unable to Destroy individual object from prefab object 0 Answers

Destroying Prefab Also Destroys Prefab Reference 1 Answer

How to destroy instantiated objects. 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