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 luniac · Jul 10, 2012 at 01:09 PM · triggerenemyray

enemy aggro region definitive answer

hi ive been looking around for a good solution for implementing a region around an enemy, that the player must enter to aggro the enemy and begin the enemys attack function.

Ive seen stuff with trigger colliders on a child object of the enemy and using sendmessage. ive seen a ray suggestion.

I want to know, what is the proper way of implementing something like this in unity. thanks.

For my particular enemy, i want it to charge in a straight line directly at the point of aggression, i dont know how that would change the implementation or not...

Comment
Add comment
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

1 Reply

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

Answer by whydoidoit · Jul 10, 2012 at 01:31 PM

So Raycasts are quite expensive, so normally you would only do them if a range check or Trigger indicated the then enemy was likely to be able to attack. If your enemy's desire to attack is based on line of sight then you will need to do a raycast or maybe a capsule cast to see if the player is reachable. Doing that for all enemies all of the time is a waste though, so I would combine it with a collider as that at least let's you visualise the area of attack.

Comment
Add comment · Show 8 · 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 luniac · Jul 10, 2012 at 07:58 PM 0
Share

hmm thats a little confusing for me. I was thinking of making a box trigger around the enemy using an empty gameObject child as the trigger container(the enemy itself has a standard collider). I could size it so that it extends only to the sides, not up or down, to prevent trigger collisions if the player is above or below the enemy.

If the player enters the trigger, a ray could be shot to that direction and if theres contact with the "player" tag, then the attack function is run.

Does this sound like a proper solution? and if it is, then how do shoot a ray to where the player collided with trigger? Can i do something like:

 if trigger entered by player 
 ray direction = player position - enemy position; //Would this work? im not expert on vectors 
 shoot one ray in ray direction 
 if hit gameobject tag = player do attack function?
avatar image whydoidoit · Jul 10, 2012 at 08:21 PM 0
Share

Yes that's the right idea - but you might use a capsule cast to ensure that the enemy can actually make it to the player.

   function OnTriggerEnter(other: Collider)
    {
         if(other.gameObject.tag == "player") {
               var hit : RaycastHit;
              if(Physics.Raycast(transform.position, other.gameObject.transform.position - transform.position, 1000, hit)) {
                if(hit.gameObject.tag == "player") {
                     //do attack
                  }
          }
    }
avatar image luniac · Jul 10, 2012 at 08:34 PM 0
Share

hey thanks a lot for the code, will save me good time. Its okay if the enemy can't make it, it will create humorous situations :)

but for future reference though just so i understand, a capsule cast is like ins$$anonymous$$d of shooting a single tiny ray, it shoots an actual capsule? like a ball shaped object(deter$$anonymous$$ed by parameters of course, preferably sized to the enemy gameobject)

avatar image whydoidoit · Jul 11, 2012 at 08:32 AM 1
Share

Yes that is right - it shoots something to show whether a character can actually make it (through potentially narrow spaces) - rays are just a line - but a capsule represents the actual size of the body. Can be very helpful to avoid "strange" situations!

avatar image psychentist · May 04, 2013 at 08:23 PM 0
Share

Okay, here's the issue I'm having. I have the sphere trigger collider as a child object of my foe character, but the OnTriggerEnter is only being called when I actually collide with the character controller, and the collider is being ignored, even though the collider and the script are both on the child object, and the character controller is on the parent object.

In short, how do I specify WHICH trigger, since it seems to think the trigger I'm referencing is the controller.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is there a way to tell which way an enemy is going? 1 Answer

(2d) Enemy not killing player if player stand still? 1 Answer

How to handle multiple colliders for one enemy and bullet. 3 Answers

How do I have multiple AI that follow the player but don't move into each other? 1 Answer

Enemy (with state machine) cannot target player!! 0 Answers


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