Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Sakul156 · May 20, 2021 at 02:13 PM · aienemyspherecheck

How can I check if my Player leaves a Sphere?

Hello there. I am currently working on my first unity-project (3D). So far i have a working CharacterController and an Enemie which is Patroling if the Player is outside of a given sphere around the enemie. If the Player enters the Sphere, the enemie starts chasing the player until he leaves the sphere again. All that works perfectly fine, but i dont like that the enemie starts patroling right away after the player leaves the sphere. I want it to stand still for a moment (and later animate to look around). To achieve that, i have to chech if and when the player leaves the sphere. I obviously already have a bool which checks if the player is inside the sphere or outside, but i have no idea how to check when the bool turns from true to false again.

Im looking forward for a few helpfull answers

best regards, Sakul156

Thanks in Advance! :)

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

2 Replies

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

Answer by Earthshine · May 20, 2021 at 08:01 PM

You just need a flag. Let it be bool isChasingPlayer for Enemy chasing Player.

 If (Player is inside the sphere and isChasingPlayer is false) 
 { isChasingPlayer becomes true;
   Enemy stops all coroutines;
   and starts the chasing coroutine }
 
 If (Player is outside the sphere and isChasingPlayer is true) 
 { isChasingPlayer becomes false;
   Enemy stops all coroutines;
   and starts looking around coroutine }

When looking around coroutine ends, Enemy restarts the patrolling coroutine.

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 Sakul156 · May 23, 2021 at 02:49 PM 0
Share

Thank you, i used your "Code" for inspiration and managed to making it work. Thanks a lot for all the help, guys!

best regards

Sakul156

avatar image Earthshine Sakul156 · May 24, 2021 at 01:54 PM 0
Share

It was kinda confusing for me to read someone else's code when I had started learning unity, so I decided to just convey the logic instead. I hope reading this 'code' wasn't too much of a headache for you.

avatar image
1

Answer by amiel_ace · May 20, 2021 at 02:20 PM

You should look at OnTriggerExit and OnTriggerEnter, from there you can set your bool

Comment
Add comment · Show 5 · 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 Sakul156 · May 20, 2021 at 02:26 PM 0
Share

hay, thanks for the fast answer. I started watching a video about those triggers and he said that i need to have a rigidbody. The problem is that i dont use a rigidbody in the project.

avatar image amiel_ace Sakul156 · May 20, 2021 at 02:36 PM 0
Share

Well, I must say, that using a rigidbody is so much easier for that purpose. But if you're really insisting in not using a rigidbody, What you can do is use and provide Physics.SphereCast, and place that in a coroutine for manually a constant checking that Physics.spherecast.

avatar image Sakul156 amiel_ace · May 20, 2021 at 02:48 PM 0
Share

Thats actually what im doing:

PlayerInSightRange = Physics.CheckSphere(transform.position, SightRange, LayerMaskPlayer);

But the problem is that i dont know how to check if i leave the sphere. I mean yeah of course the bool turns to false again but i dont know how to include that into a function.

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

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

Related Questions

AI Script attached to Enemy and is Rotating around player 0 Answers

Disable a target after trigger exit? 1 Answer

How can I ground enemies and use target transform? 1 Answer

transform set for an enemy to follow doesnt work when enemies are spawned 1 Answer

Rotating Wrong 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