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
0
Question by Bullet0070 · Nov 09, 2013 at 08:43 PM · buggun

Picking up ammo from the weapon on the ground

Hello! Maybe you now the guy ETeeski. So, i'm making a FPS game using his tutorials. Work pretty good, but i have a problem. ETeeski didn't said anything about it in his tutorials, but it exists. I learned how to pickup ammo from weapon that is on the ground. So, i have a pistol in my hand, i pass over the gun on the ground and my gun takes ammo from that gun on the ground. Works good. But i can pickup ammo FROM ALL the guns. I can take ammo for my pistol just passing over a RIFLE. It seems not to be good. My idea is to check if the String name of my current gun equals the name of the gun wich is on the ground. And if their names equals to each other, the ammo would be taken. And i dont know how to do this(( Here is the video. http://www.youtube.com/watch?v=3dI7Tm3p2SM&list=PL7AE076AFAFD3C305 And here you can find a code. http://forum.unity3d.com/threads/124970-ETeeskiTutorials-Scripts-up-to-FPS1-29 Search at the bottom of "PlayerMovementScript". PLEASE HELP!Sorry for my english.

Comment
Add comment · Show 4
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 ahaykal · Nov 09, 2013 at 09:39 PM 0
Share

People come here thinking if they get this script they will finish their game and then after a day or two they just trash it. Guys learn to code, learn the fundamentals and THEN make your game. Please!

avatar image Huacanacha · Nov 09, 2013 at 09:52 PM 0
Share

Look at the Unity docs. Object.name gives you the name of the item, GameObject.tag is a tag you can give objects. Ideally your weapons would have an ammunition type that could be compared, ass this is what you are really trying to check. If you have a base class for weapons just add a field for this. You could use an enum to specify all the ammo types efficiently.

avatar image Bullet0070 · Nov 09, 2013 at 10:31 PM 0
Share

I have 2 objects, the "currentGun" and "gun". They both are GameObjects. I wanted to check if they equals to each other. if(currentGun == gun) { } But it doesn't work( I can't pickup ammo at all.

avatar image Huacanacha Bullet0070 · Nov 10, 2013 at 05:48 AM 1
Share

This is not an answer. Please use the site properly.

How do you want to deter$$anonymous$$e if currentGun is equal to gun? Do you mean whether they are the same instance (they won't be given you description), because that's what using == on objects will tell you. If you mean you want to know if the type of gun is the same (so they can use the same ammo), you need to tell us how you specify the gun type. Do you have a variable with the type of gun? Do you have a different class for each gun? Does each gun of a certain type have the same tag?

Show us some of your code where you try to do this, and tell us which parts don't work as expected. We can't deter$$anonymous$$e your problem based on snippets of single lines of code.

Edit: I converted your answer back to a comment for you.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bullet0070 · Nov 10, 2013 at 08:42 AM

At first, guys, i'm not thinking that i can make my game in 2 days. NO. I understood my problem and tried to set the tags for each of gunType. I set the tag, but i din't exactly know how to use it. I checked the gun to have certain tag. I didn't worked, i did something wrong(. Here is my code of picking up gun when it passed:

function OnCollisionExit () { grounded = false; }

function OnTriggerStay (hitTrigger : Collider) { var current : SMGGunScript = null; if (currentGun) current = currentGun.GetComponent(SMGGunScript); var ammo : AmmoPickupScript = null; var gun : SMGGunScript = null;

 if (hitTrigger.tag == "AmmoPickup" && waitToPickupAmmo <= 0)
 {
     ammo = hitTrigger.gameObject.GetComponent(AmmoPickupScript);
     if (current.currentExtraAmmo < current.maxExtraAmmo)
     {
         if (ammo.fromGun)
         {    
             gun = ammo.gun.GetComponent(SMGGunScript);
             if (gun.currentExtraAmmo > 0 && gun.ammoType == current.ammoType && ammo.gun != currentGun)
             {
                 if (gun.currentExtraAmmo >= current.maxExtraAmmo - current.currentExtraAmmo)
                 {
                     gun.currentExtraAmmo -= current.maxExtraAmmo - current.currentExtraAmmo;
                     current.currentExtraAmmo = current.maxExtraAmmo;
                 }
                 if (gun.currentExtraAmmo < current.maxExtraAmmo - current.currentExtraAmmo)
                 {
                     current.currentExtraAmmo += gun.currentExtraAmmo;
                     gun.currentExtraAmmo = 0;
                 }
                 if (ammo.pickupSound)
                     ammo.gameObject.GetComponent(AudioSource).Play();
             }
             
         }
         if (!ammo.fromGun)
         {
             if (current.ammoType == ammo.ammoType || ammo.ammoType == -1)
             {
                 if (ammo.ammoAmount > 0 && !ammo.unlimitedAmmo)
                 {
                     if (ammo.ammoAmount >= current.maxExtraAmmo - current.currentExtraAmmo)
                     {
                         ammo.ammoAmount -= current.maxExtraAmmo - current.currentExtraAmmo;
                         current.currentExtraAmmo = current.maxExtraAmmo;
                     }
                     if (ammo.ammoAmount < current.maxExtraAmmo - current.currentExtraAmmo)
                     {
                         current.currentExtraAmmo += gun.currentExtraAmmo;
                         ammo.ammoAmount = 0;
                     }
                     if (ammo.pickupSound)
                         ammo.gameObject.GetComponent(AudioSource).Play();
                 }
                 if (ammo.unlimitedAmmo)
                 {
                     current.currentExtraAmmo = current.maxExtraAmmo;
                     if (ammo.pickupSound)
                         ammo.gameObject.GetComponent(AudioSource).Play();
                 }
             }
         }
     }
 }

}

My problem is i can't check the gun on the ground. "currentGun" is my gun i'm holding. "gun" as i can see, is a weapon on the ground. Both they are GameObject. I tried to use this: if(currentGun == gun) { }

And i tried to use their names: if(currentGun.name == gun.name) { } I hope you will understand me.

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 Huacanacha · Nov 10, 2013 at 09:25 AM 0
Share

You did it again. Please do not post an Answer to make a comment. Add your comments to the question section or to the relevant answer.

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

18 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Gun lag behind camera 1 Answer

Play Specific Animations of an Object 1 Answer

Toggle weapon Script. 1 Answer

Weird Glitch When Instantiating a Game Object 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