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 thornekey · Jan 13, 2014 at 07:25 AM · enemy

Destroy object when not looking

How do i make it so that when a player rotates their view a certain amount (possibly rotation view like 180) an object will be destroyed.

basically im making an enemy chase a player, and if the player turns away i want the enemy to disappear (destroy)

thinkin maybe a ray cast... idk

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 sanmn19 · Jan 13, 2014 at 07:59 AM

You can try the OnBecameInvisible() method under MonoBehaviour. Attach this to the player. This is assuming the player is a first person view. If its a 2d view with the bot chasing the player, then you can check for the specific range of angle in Update() function and destroy the bot.

 GameObject bot;
 
 void OnBecameInvisible(){
     Destroy( bot );
 }
Comment
Add comment · Show 3 · 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 thornekey · Jan 13, 2014 at 08:06 AM 0
Share

thanks for the quick reply. yes its a FPS. although in the update function how do i check the rotation? cos it would be the rotation of the camera in relation to the enemy.. thanks for putting it in C# too

avatar image GameVortex · Jan 13, 2014 at 08:33 AM 1
Share

Actually that script should be placed on the enemy itself. Because then it will be called as soon as the enemy is no longer being rendered by the camera.

So you can just do:

 private void OnBecameInvisible()
 {
     Destroy( gameObject );
 }

Or if you do not want the enemy to be destroyed that quickly, you could do a quick check in the update ins$$anonymous$$d to see if the enemy is behind or in front of the camera. This may work or not depending on the size of the enemy and the field of view of the camera.

 private void Update()
 {
 if(fpsCamera.transform.InverseTransformPoint(transform.position).z < 0)
     {
         Destroy(gameObject);    
     }
 
 }

This transform the point of the enemy into local space for the camera, then it is easy to check if the enemys z position is negative and therefore behind the camera.

avatar image thornekey · Jan 13, 2014 at 08:55 AM 0
Share

thanks heaps - this has helped a lot

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

20 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

Related Questions

Creating basic enemy animation and attacking 4 Answers

(C#) My enemy dosent take damage ScriptFix 1 Answer

How to make a Tron Light cycle Enemy AI Script ? 0 Answers

Enemy not following player. 2 Answers

get closest enemy 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