- Home /
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.
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!
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.
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.
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.
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.
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
Follow this Question
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