- Home /
Buff System
I want to make a buff system so when i walk over a object it buffs me, a bit like a health pack thing, where you walk over a health pack and you gain health but for a var called weaponStrenth. im new to coding and really want to become good at it,but i cant find any help on this anywere. All help is usefull :) Thanks.
Answer by Hybris · May 05, 2012 at 06:52 PM
I don't know what you mean by "weaponStrength", but I will give you a code anyways.
This is going to be a huge answer, but helpfull for someone just started coding, with extra explanation and stuff.
Well, for this we use a collider so:
-Add collider(mesh, box, or whatever you want to use, no rigidbody!)
-Set the collider to "isTrigger", that way you don't bounce off the object
-Create a tag, and call it "Pickup" for now, you can always change it
-Add the following code to your player
I will give you a code to apply to the player,and I will give you a code wich you can use to apply to the pickup object, you will have to choose 1 AND(yes there's more!) I will give you another script wich you can use in combination with the script following and it's called pickupscript for now! More info after the main code!
-So, add this code:
var weaponStrenght : int;
function OnTriggerEnter(hit : Collider //this is to define wich object you hit!){
var pickupscript = hit.GetComponent(pickupscript);
if(hit.transform.tag == "Pickup"){
pickupscript.BuffAmount += BuffAmount;//I'm calling a static value in the pickup script
//pickup script will come after this
}
}
A little extra explanation: I'm using the OnTriggerEnter function wich will check if you have entered a collider that is a trigger(note that we just set the collider of the pickup as a trigger!).
I used hit.GetComponent(pickupscript), the hit is for the collider we hit, in the object we hit, we check if it has a component called pickupscript(we will create this soon!)
We use that script to acces a variable, actually it's a static variable(so we can acces this very easy, lolz I just found out that you don't need the component, but it could be more easy and a good example to use later!)
Now.....it's time.....THE PICKUPSCRIPT!!!!!
static var BuffAmount : int = 10;
//and you could add some extra variables!
This is all you need! BTW: I'm not sure if it needs to be static, check plz if it has to be static you can't acces it from the inspector, but there's a workaround and here it is!:
static var BuffAmount : int = 10;
var BuffAmount_holder : int = 10;
function Start(){
BuffAmount = weaponStrenght_holder;
}
Add the main script to the player and the pickup script to the pickup. You don't need the pickup script if you will not use it, you will have to declare the variables in the main script, if you have alot of pickup types it's easy to use 1 script to add to those pickups, or if you want big and small pickups this is easy!
Now to only use the script with the pickup, so to turn it around:
var BuffAmount;//and other buff types, I'm not going to fully declare it
function OnTriggerEnter(hit : Collider //this is to define wich object you hit!){
var playerscript = hit.GetComponent(playerscript);
if(hit.transform.tag == "Player"){
playerscript.weaponStrenght += BuffAmount;
}
}
Sorry for the crazy long answer.....this is the longest answer I ever did(I felt helpfull, and I just needed to.....SCRIPT ALL THE SCRIPTS!!!!). If you give me some specific info, I will see what I can add, edit and how I can help you!
Good luck, -Hybris
Hey, Thank you for taking the time to put such a long awnser :) The script so far has some issue in my game. so here is some more info. the weaponStrength is my var which will tell my projectile how much damage to do. my char is simply called Player and the movment script is called PlatformerController. and im not sure if var's weaponStrenght and weaponStrenght are just spelling mistakes. Thanks :)
then what does weaponStrenght have to do with the buff system?
The WeaponStength var is what will be increased from the Damage Script hence the buff to incease the damage done by my weapon. so basicly i want to walk over the object and mkae it so the object diserpears and the weaponStrenth var will be inscreased by a small amount. e.g. var weaponStrength : int = 1 but after walking over the buff it becomes var weaponStrength : int = 2 :) thanks again. :)
The 'var' means 'variable'- you don't have to declare it twice for it to work like that! All you need to do is this:
weaponStrength += buffAmount;
where buffAmount is the amount of strength you want to add.
Remember to keep track of the base amount, if you want the buff to wear off after a while.
ok, you can still use my code, if you modify it a bit. Ins$$anonymous$$d of the weaponstrenght var in the main script you should have buffAmount as @syclamoth said. Then you get your script by using: var weaponS = GetComponent(weaponscript);. Then: weaponS.weaponStrenght += buffAmount also as @syclamoth said(srry sycla that im repeating some stuff you said) With this said, you can do what you want. If you want me to modify my answer, just shout and I will see when I can edit it! Good luck once again!
Your answer
Follow this Question
Related Questions
Weapon Pickup and change 2 Answers
How do you Find Object through tags in javascript? 3 Answers
Respawning script question. 1 Answer
How to pick / drop object 1 Answer
Play Audio While Button is Held Down 1 Answer