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 Gale · May 04, 2013 at 06:00 AM · rpgcommunicationcombat

Communicating variables between Scripts for Attacks

So I'm working on an RPG Action game and I have the combat mechanics set up to an extent. Right now what I have is the player who is attacking enemies with projectiles. It works fine but I can't seem to get the more RPG aspect of it to work. I have the player who has different stats depending on level and such (Attack, Special Attack) and I want those stats to be calculated against the enemies (Defense, Special Defense) as well as which projectile the enemy was hit with (Such as a Fireball or a Lightning Bolt). So basically I have 3 different stats on 3 different scripts and I can't figure out an effective way to get them all into the calculations for the Enemy's Damage function.

I can't just make the players variables global because the player can switch which character they are using and each character has several projectiles. So basically I need a way for the Enemy's Damage function to find out which character fired the projectile, get his stats, figure out what the stats of the projectile are, and then use both those stats as calculations into the function itself.

These are my classes as of right now. A BaseAttributes is created on each character and enemy and a AttackAttributes is created on the prefabs for the projectiles. I have a simple "Hit" Function on the BaseAttributes that goes off when a projectile hits it, I just need help figuring out how to transfer the variables around.

 class BaseAttributes
 {
     var name             : String;
     var level            : int;
     var hp                : Stat = new Stat();
     var exp                : Stat = new Stat();
     var typeOne            : int;
     var typeTwo            : int;
     
     var normalAttack    : int;
     var specialAttack        : int;
     var normalDefense    : int;
     var    specialDefense    : int;
     var speed            : int;
     
     function BaseAttributes ( name : String, level : int, hp : int, exp : int, typeOne : int, typeTwo : int, normalAttack : int, specialAttack : int, normalDefense : int, specialDefense : int, speed : int)
     {
         this.name = name;
         this.level = level;
         this.hp.max = hp;
         this.hp.current = hp;
         this.exp.max = exp;
         this.exp.current = exp;
         this.typeOne = typeOne;
         this.typeTwo = typeTwo;
         this.normalAttack = normalAttack;
         this.specialAttack = specialAttack;
         this.normalDefense = normalDefense;
         this.specialDefense = specialDefense;
         this.speed = speed;
     }
 }
 
 class AttackStats
 {
     var name            : String;
     var attackType        : int;
     var attackElement    : int;
     var pp                : Stat = new Stat();
     var baseDamage        : int;
     
     function AttackStats ( name : String, attackType : int, attackElement : int, pp : int, baseDamage : int)
     {
         this.name = name;
         this.attackType = attackType;
         this.attackElement = attackElement;
         this.pp.max = mp;
         this.pp.current = mp;
         this.baseDamage = baseDamage;
     }
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 FL · May 05, 2013 at 05:39 PM

Use the projectile as a collider. When the projectile collide, just pick the script componet of the colliders and apply the damage (pass the attributes or a reference to your class in the projectile).

Read the first and the third topics at http://unitygems.com/mistakes1/

Comment
Add comment · Show 2 · 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 Gale · May 05, 2013 at 06:40 PM 0
Share

Thanks for the answer. And yeah that's kinda what I ended up doing.

Since every character has different stats what I ended up doing is creating scripts for each separate characters base stats and calculations. And then I created a generic "Battle Stats" script that gets fed the important information for the Battle. Then whenever something collides with the enemy the Enemy script pulls a script from the collider, searches for whichever character is currently being controlled and pulls the generic battle stats script off them and uses the collider script and the battle stats script to do the calculations for damage.

avatar image ExTheSea · May 05, 2013 at 06:42 PM 0
Share

I converted your answer to a comment because the answers are only used to answer the main question.

  • Read this page : http://answers.unity3d.com/page/newuser.html

  • Please watch : http://video.unity3d.com/video/7720450/tutorials-using-unity-answers

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

13 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

Related Questions

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

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Communication between objects and other scripts, variables and properties 1 Answer

Material doesn't have a color property '_Color' 4 Answers


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