- Home /
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;
}
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/
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.
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