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 $$anonymous$$ · Aug 07, 2013 at 05:43 PM · enemystopvisibleslenderman

Making an Enemy stop when visible to Player...

I have a script to make my enemy simply follow the player

 var rotationSpeed = 3; //speed of turning
  
 var myTransform : Transform; //current transform data of this enemy
 var target : Transform; //the enemy's target
 
 var moveSpeed = 5;
 
 
 function Awake()
 {
     myTransform = transform; //cache transform data for easy access/preformance
 }
  
 function Start()
 {
      target = GameObject.FindWithTag("Player").transform; //target the player
  
 }
  
 function Update () {
     //rotate to look at the player
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
     Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
  
     //move towards the player
     myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
     }
     

However i've spent ages trying to work out how i can make it so that when the enemy is visible to the player the enemy stops moving. I've looked into raycasting etc but can't seem to get it right. If someone could write a script that somehow changes the move speed variable to 0 when the enemy is visible to the player i would greatly appreciate it , Thanks =D

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

3 Replies

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

Answer by robhuhn · Aug 08, 2013 at 10:44 AM

If the player can actually see with a camera attached you could use renderer.isVisible or OnBecameVisible

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 $$anonymous$$ · Aug 08, 2013 at 12:37 PM 0
Share

If the enemy is rendered this would mean he's visible? Wouldn't that mean he would always be moving

avatar image robhuhn · Aug 08, 2013 at 01:53 PM 0
Share

renderer.isVisible is true if the enemy is rendered by any camera (within the cameras frustum). So if I get you right you could attach a script to the enemy:

     function OnBecameVisible() {
         Freeze();
     }
 
     function OnBecameInvisible() {
         $$anonymous$$ove();
     }

And keep in $$anonymous$$d that the scene view cam also forces to call these methods:

Note that object is considered visible when it needs to be rendered in the scene. It might not be actually visible by any camera, but still need to be rendered for shadows for example. Also, when running in the editor, the scene view cameras will also cause this function to be called.

avatar image $$anonymous$$ · Aug 08, 2013 at 02:28 PM 0
Share

Thanks for the script however it comes up with

Assets/Thestopscript.js(4,20): BCE0043: Unexpected token: :.

There's something wrong with the colon? obviously i'm new to coding in unity ;)

avatar image robhuhn · Aug 08, 2013 at 02:47 PM 0
Share

sorry my script was c#. It's changed now

avatar image $$anonymous$$ · Aug 08, 2013 at 04:18 PM 0
Share

I FINALLY F*C$$anonymous$$*G DID IT 2 DAYS OF WOR$$anonymous$$ING ON THIS

avatar image
0

Answer by creighcl · Aug 07, 2013 at 06:04 PM

You're on the right track. You'll want to use a raycast that zips from the badguy's position to the player's position and see what is returned by the raycast. If its the player, then there's nothing in between to obstruct visibility.

Link below to show you how this is done.

You'll also want to find ways to only do these raycast checks only when the two are within a certain a reasonable distance first. Otherwise enemies will always stand still if there's nothing standing between themselves and the player.

Note: Raycast is a method to detect COLLIDERS, not gameobjects. Therefore, this is only going to work if you're working with colliders on your characters.

Related Post : http://answers.unity3d.com/questions/15636/how-can-i-check-the-line-of-sight-to-see-if-the-pl.html

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 $$anonymous$$ · Aug 08, 2013 at 10:22 AM 0
Share

Thanks for the reply i'll see how I get on with this

avatar image
0

Answer by Eclipsed · Aug 08, 2013 at 11:05 AM

Just raycast from the player to the enemy, if the raycast is successful then calculate the angle between the forward of the player and the ray you used for the raycast.

If this angle falls between a certain threshold (something like +-45 degrees) then the player can see the enemy.

To calculate the angle use Mathf.Atan2(), check on wikipedia how this function works ;)

Cheers.

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

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

16 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

Related Questions

AI enemy Stuck at corners 2 Answers

Invisible objects renderer disable? How to stop this? 0 Answers

animation on multiple objects 1 Answer

How to see if a transform is visible without a camera 1 Answer

Enemy is not stopping following the player 2 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