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 AngryCSharpNerd · Aug 20, 2013 at 06:02 PM · gameobject.findswordswinging

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);
         }
     
 
Comment
Add comment · Show 5
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 0tacun · Aug 20, 2013 at 07:45 PM 2
Share

I'm scripting my AI at the moment, and this will be my approuch:

  1. Check if Player is in Field of Vision.

  2. Raycast him.

  3. Get his script with hit.transform.GetComponent.

  4. Check if in Player's script the boolean isSwinging true.

  5. Use this boolean in Enemy script to call Dodging(); .

avatar image AngryCSharpNerd · Aug 20, 2013 at 08:18 PM 0
Share

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

avatar image 0tacun · Aug 20, 2013 at 08:32 PM 0
Share

Is this script attached to the player or enemy?

avatar image AngryCSharpNerd · Aug 21, 2013 at 01:15 AM 0
Share

I'm sorry, this is attached to the enemy

avatar image 0tacun · Aug 21, 2013 at 09:41 AM 0
Share

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?

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 6 · 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 AngryCSharpNerd · Aug 21, 2013 at 02:44 PM 0
Share

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[];
avatar image ArkaneX · Aug 21, 2013 at 03:12 PM 0
Share

Please check if swordPlay.Length == 2.

avatar image AngryCSharpNerd · Aug 21, 2013 at 03:22 PM 0
Share

Yeah, something's definitely wrong. It can even print(swordPlay.Length) without an error, even with using the Find() or the FindGameObjectsWithTag()..

avatar image ArkaneX · Aug 21, 2013 at 03:35 PM 0
Share

Answer was updated - sorry for confusion.

avatar image AngryCSharpNerd · Aug 22, 2013 at 03:37 AM 0
Share

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
         }
Show more comments

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

17 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 avatar image avatar image avatar image avatar image

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


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