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 Calumcj · May 04, 2012 at 07:35 PM · javascriptobjectgamepickup

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.

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
0

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

Comment
Add comment · Show 7 · 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 Calumcj · May 07, 2012 at 03:44 PM 0
Share

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

avatar image Hybris · May 09, 2012 at 02:12 PM 0
Share

then what does weaponStrenght have to do with the buff system?

avatar image Calumcj · May 10, 2012 at 09:50 AM 0
Share

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

avatar image syclamoth · May 10, 2012 at 09:53 AM 0
Share

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.

avatar image Hybris · May 10, 2012 at 01:55 PM 0
Share

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!

Show more comments

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


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