Assuming an RPC call is the correct way to do this...here is my pseudocode. The problem is that RPC "RemoteApplyDamage" is executed on all clients. I want it to execute just on the clients who are in attack range. Is there some ID I can use to identify the Player(s) to damage? I have tried using gameObject.GetInstanceID() but the local InstanceID of a game object seems to be different than the remote InstanceID of the same game object. I need to be able to handle both scenarios - enemies within the level (bots) and also enemies who are network players.
var AttackPowerJab:int;
var AttackPowerWhirl:int;
var AttackPowerCombo:int;
var attackPosition = new Vector3(0,0,0.8);
var attackRadius = 1.3;
private var attackJabHitTime = 1.0;
private var attackWhirlHitTime = 1.8;
private var attackComboHitTime = 1.8;
private var attackJabTime = 1.5;
private var attackWhirlTime = 2;
private var attackComboTime = 2;
private var waitHitTime = 0;
private var waitTime = 0;
private var attackPower = 0;
private var busy = false;
static private var InstanceID:int;
function Start(){
InstanceID = transform.gameObject.GetInstanceID();
}
function Update(){
if (Input.GetButton ("Jab")){
waitHitTime = attackJabHitTime;
waitTime = attackJabTime;
attackPower = AttackPowerJab;
if (!busy){
busy = true;
Attack();
}
}
else if (Input.GetButton ("Whirl")){
waitHitTime = attackWhirlHitTime;
waitTime = attackWhirlTime;
attackPower = AttackPowerWhirl;
if (!busy){
busy = true;
Attack();
}
}
else if (Input.GetButton ("AttackCombo")){
waitHitTime = attackComboHitTime;
waitTime = attackComboTime;
attackPower = AttackPowerCombo;
if (!busy){
busy = true;
Attack();
}
}
}
function Attack(){
yield WaitForSeconds(waitHitTime);
var pos = transform.TransformPoint(attackPosition);
// enemies
var enemies : GameObject[];
enemies = GameObject.FindGameObjectsWithTag("Enemy");
for (var go : GameObject in enemies) {
var enemy = go.GetComponent(EnemyLifeMeter);
if (enemy == null)
continue;
Debug.Log("Distance: " + Vector3.Distance(enemy.transform.position, pos));
//Debug.Break();
if (Vector3.Distance(enemy.transform.position, pos) < attackRadius){
if (Input.GetButton ("Jab") || Input.GetButton ("Whirl") || Input.GetButton ("AttackCombo"))
enemy.SendMessage("ApplyDamage",attackPower);
}
}
// enemies that are network players
var players : GameObject[];
players = GameObject.FindGameObjectsWithTag("Player");
for (var go : GameObject in players) {
/* var player = go.GetComponent(PlayerLifeMeter);
if ((player == null))
continue;
*/
Debug.Log("Game Object: " + go.GetInstanceID());
Debug.Log("Me: " + InstanceID);
//Debug.Break();
if ((Vector3.Distance(go.transform.position, pos) < attackRadius) && !(go.GetInstanceID() == InstanceID)){
if (Input.GetButton ("Jab") || Input.GetButton ("Whirl") || Input.GetButton ("AttackCombo")){
//go.SendMessage("ApplyDamage",attackPower);
networkView.RPC ("RemoteApplyDamage", RPCMode.Others, attackPower, go.GetInstanceID());
}
}
}
yield WaitForSeconds(waitTime - waitHitTime);
busy = false;
}
function OnDrawGizmosSelected(){
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(transform.TransformPoint(attackPosition), attackRadius);
}
@RPC
function RemoteApplyDamage (liferested:int, instID:int) {
//playerObject = GameObject.FindWithTag("Player");
//playerObject.SendMessageUpwards("ApplyDamage",liferested,SendMessageOptions.DontRequireReceiver);
//var gos : GameObject[];
//gos = GameObject.FindGameObjectsWithTag("Player");
//for (var go : GameObject in gos) {
//Debug.Log("InstID: " + instID);
//Debug.Log("go.InstID: " + go.GetInstanceID());
//Debug.Log("Name: " + go.name);
//if ((transform.gameObject.GetInstanceID() == instID)) {
//Debug.Log("-----------------Attacking: " + transform.gameObject.name);
SendMessageUpwards("ApplyDamage",liferested,SendMessageOptions.DontRequireReceiver);
//break;
//}
//}
}