- Home /
Detecting when player swings weapon(s)
So, I have a problem and I don't know how to write this: I have two weapons, a switchblade and a sword. What I want to do for the enemy is to detect when either one is swinging a sword for it to dodge. Unfortunately, I don't know how to write that.
var swordPlay = GameObject.Find("blade").transform.parent.GetComponent(WepProperty).isAttacking; //I have two "blades", so this variable will only get one. When I swing one of the weapons, it's own WepProperty script isAttacking variable will turn true.
//I'm not sure how I could play GameObject.FindGameObjectsWithTag() into this dilemma because I'll need to use a for() statement and i don't think it would be a good idea to do so, unless if you know how to make it work well
//I have variables already defined also, and you don't need to check the script for errors
if(!swordPlay || (swordPlay && range >= backrange)){ //approaching player
transform.LookAt(player.transform);
var moveDirection:Vector3 = transform.TransformDirection(0,0,1);
moveDirection.y -= gravity;
controller.Move(moveDirection*Time.deltaTime*speed);
db = 0;
}
else if(swordPlay && range <= backrange && relativePoint.z > 0.0){ //dodging player when he swings his weapons
transform.LookAt(player.transform);
var backwards:Vector3 = transform.TransformDirection(randomdodge);
backwards.y -= gravity;
controller.Move(backwards*Time.deltaTime*speed);
}
I'm scripting my AI at the moment, and this will be my approuch:
Check if Player is in Field of Vision.
Raycast him.
Get his script with hit.transform.GetComponent.
Check if in Player's script the boolean isSwinging true.
Use this boolean in Enemy script to call Dodging(); .
Yes, that's basically what I have when it comes to colliding (except for the Raycasting; I've never used it before or know how it's used) but this script uses Update() so I'm not quite sure how I can detect one of the objects. ..I probably should have mentioned that..
Hmm, I'm sorry i can't find the error. Did you tried just checking if the enemy is responding on swordPlay? Without the range checking?
Answer by ArkaneX · Aug 21, 2013 at 11:11 AM
I believe you can achieve this by getting all WepProperty components of the player and testing isAttacking of each:
var weaponInfos = GameObject.Find("player").GetComponents(WepProperty) as WepProperty[];
if(weaponInfos[0].isAttacking || weaponInfos[1].isAttacking)
{
// dodge
}
I assumed here, that the player always has two WepProperty components.
Btw - I believe it's a good idea, to do GameObject.Find("Player") once (in Awake or Start) and store the result in some variable. If player object can be destroyed and recreated, then the variable should be reinitialized.
EDIT: By "player" I understand GameObject that you find using
GameObject.Find("blade").transform.parent
EDIT 2:
Oops - sorry - mistake in my code. You have to use:
var weaponInfos = GameObject.Find("player").GetComponents(WepProperty);
var weaponInfo1 : WepProperty = weaponInfos[0];
var weaponInfo2 : WepProperty = weaponInfos[1];
if(weaponInfo1.IsAttacking || weaponInfo1.IsAttacking)
{
// dodge
}
In my previous code casting to WepProperty[] caused weaponInfo to be null (it is invalid cast, because GetComponents in JavaScript always return Component[]).
I suggest retrieving weapon components once, and storing them for later use. Just like I suggested to store player variable above.
This seems like it should work, but actually, the blade.transform.parent is an empty gameobject with other sword scripts attached. I'm actually getting a null reference exception right now.
var swordPlay = GameObject.Find("blade").transform.parent.GetComponents(WepProperty) as WepProperty[];
if((!swordPlay[0].isAttacking || !swordPlay[1].isAttacking) || ((swordPlay[0].isAttacking || swordPlay[1].isAttacking) && range >= backrange)){ //NullReferenceException on this line
//blah
}
I tried doing FindGameObjectsWithTag ins$$anonymous$$d of a Find() but I got a $$anonymous$$issing$$anonymous$$ethodException (probably because I'm not doing it right):
GameObject.FindGameObjectsWithTag("swordParent").GetComponents(WepProperty) as WepProperty[];
Yeah, something's definitely wrong. It can even print(swordPlay.Length) without an error, even with using the Find() or the FindGameObjectsWithTag()..
Well, I GOT IT! Ins$$anonymous$$d of using GetComponents(), I used GetComponentsInChildren, so here's basically my code if you want to see it:
var weaponInfos = GameObject.FindWithTag("Player").GetComponentsInChildren(WepProperty);
var weapon1 : WepProperty = weaponInfos[0];
var weapon2 : WepProperty = weaponInfos[1];
if((!weapon1.isAttacking && !weapon2.isAttacking) || ((weapon1.isAttacking || weapon2.isAttacking) && range >= backrange)){
//follow
}
else if((weapon1.isAttacking || weapon2.isAttacking) && range <= backrange && relativePoint.z > 0.0){
//dodging
}
Your answer
Follow this Question
Related Questions
How do I make a sword swing? 2 Answers
What is the best way to do third person melee combat? 1 Answer
Swinging a sword without an animation... 1 Answer
Procedural Sword Fighting on a mobile device? 0 Answers
GameObject.Find() Question 1 Answer