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 CG-DJ · Apr 17, 2013 at 03:50 PM · colliderdetectmeleecombatsword

Sword Combat System: Detect if player in range

Ok, guys, I've been Googling, testing, and experimenting for the past 2 hours.

I have a first person character with a sword. I need him to attack the enemies and give them damage. I'm hitting a hiccup in that I can't figure out how to detect if the enemy is in range.

I think this is how I want the system to work: I have a box collider in front of my main character. I want the box to check every frame whether or not an enemy is touching the box. If there is an object with the tag "enemy" in the box, I want a boolean variable to be true, and if there is no enemy in the box, then I want the variable to be false. Right now, I can get the variable to be true when the box collides with the enemy, but I can't seem to get it to change to false.

 var enemyInRange : boolean = false;
 
 function OnTriggerEnter (other : Collider) 
 {
     if(other.gameObject.tag == "enemy")
     {
         enemyInRange = true;
     }
 }
 
 function OnTriggerExit (other : Collider)
 {
     if(other.gameObject.tag == "enemy")
     {
         enemyInRange = false;
     }
 }
 
 function Update()
 {
    // Tells me what the value of the variable is, so that I can see if it changes
    Debug.Log(enemyInRange);
 }

Thanks for the help!!!

Comment
Add comment · Show 3
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 CG-DJ · Apr 17, 2013 at 06:36 PM 0
Share

bump - sorry it took forever to get approved

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

maybe try:

 function OnTriggerStay(other : Collider) {
  if (other.gameObject.tag == "enemy")
    enemyInRange = true;
 }
 
 
 function OnTriggerExit(..) {
   enemyInRange = false;
 }
avatar image CG-DJ · Aug 21, 2013 at 03:57 AM 0
Share

Good Idea, but as Dracorat said, that won't work. An 2 enemies can walk into the trigger, and then one can walk out, and it will be set to false.

I ended up using a raycast! but thanks for the help!

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Dracorat · Apr 17, 2013 at 06:59 PM

You should probably not use true and false but rather use a count of enemies or else two can enter, one can exit and you'll be set to false.

So, make enemyInRange in to enemyInRangeCount and an int type.

Increase on Enter, decrease on Exit.

Comment
Add comment · Show 2 · 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 CG-DJ · Apr 17, 2013 at 07:16 PM 0
Share

Ok. Thanks! I'll see if it works!

avatar image CG-DJ · Apr 17, 2013 at 08:06 PM 0
Share

Yep that was it! But for anyone else reading this, make sure that the box collider you use to detect the collisions has a rigidbody attached!

avatar image
0

Answer by Yokimato · Apr 17, 2013 at 07:52 PM

If you're keeping the box collider system, I agree with @Dracorat, but I wonder about how you have your game play mechanics set up.

If you have a targeting system, i.e before I attack, I need to have selected a target. In which case, you have the target so do a Vector3.Distance check between player and target to get the distance and compare against some range value. I think this is simply and accurate.

If you don't have a targeting system, then shouldn't your character be able to swing regardless of anything around him? If he hits (meaning the collider on the weapon hits a collider on a enemy) an enemy, great---Bully for him; If he doesn't...no big deal right? The point is here "range" doesn't matter since it's tethered by the model of the weapon.

I just want to make sure we're solving a problem you actually have is all ;)

Comment
Add comment · Show 1 · 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 CG-DJ · Apr 18, 2013 at 12:41 PM 0
Share

This combat system is (i think) like Skyrim, but I don't know for sure since I haven't played the game. No, I need this "range" box because I don't want the length of the attack to be dependent on the animations. But this range box is basically as long as the sword animation.

However, I think I may use a raycast ins$$anonymous$$d. That would make you be more precise and have to click directly on the person you want to attack, ins$$anonymous$$d of just in their general vicinity. I don't know.

But thanks guys!!! Now I can choose between the better gameplay mechanic!

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

14 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

Related Questions

Multiple Melee Weapons 2 Answers

Best way to detect sword hit 0 Answers

How should i properly do sword combat? (Think: M&B/Oblivion/Skyrim) 3 Answers

Melee attack comblat 1 Answer

Melee Combat 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