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;
	}

1 Answer

1

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 unitygems.com

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

It is advisable to use 'add new comment' button to post comments for questions/answers rather than posting your comment as an answer. This time it is converted for your convenience but be sure to post comment as comment and not as answer from next time.