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 Julian_Spring · Mar 25, 2013 at 07:19 PM · javascriptaienemynavmesh

Enemy looks for you, when he can't see you

Okay I have a script when you in the line of sight he will follow you, I am also using Nav Mesh.

What is want to do is, if the Enemy cannot see the player anymore I want it to look around for a certain amount of time then go back to its starting point.

 var walkSpeed = 3;
 var rotationSpeed : float = 3;
 var runningSpeed : float = 6;
 
 var player : GameObject;
 var myTransform : Transform;
 var target : Transform;
 var fieldOfView : float = 50;
 var rayRange : float = 10;
 
 var distanceToStopRunning : float = 10;
 var distanceToStopMoving : float = 3;
 var detectFromBehind : float = 2;
 
 var sneakingBehind = false;
 var chasing = false;
 
 function Awake(){
     myTransform = transform;
 }
 
 function Update(){
 
     var hit : RaycastHit;
     var rayDirection = player.transform.position - transform.position;
     var distanceToPlayer = Vector3.Distance(transform.position, player.transform.position);
     
     //Checks if to see if player is infront
     if((Vector3.Angle(rayDirection, transform.forward)) < fieldOfView){
         if(Physics.Raycast (transform.position, rayDirection, hit, rayRange)){
             if(hit.transform.tag == "Player"){
                 Debug.Log("Can see you MF");
                 lookingAt = true;
             }
         }
     }
     
     if(lookingAt == true){
         chasing = true;
     }      
   
         
     if(chasing == true){
         GetComponent(NavMeshAgent).destination = target.position;
         if(lookingAt == false){
             
             
             
         }
     }
 }
 
 function OnDrawGizmosSelected(){
     Gizmos.color = Color.green;
     Gizmos.DrawRay (transform.position, transform.forward * rayRange);
 }



Thanks for the help.

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
0
Best Answer

Answer by Yokimato · Mar 25, 2013 at 08:06 PM

I won't write the entire thing for you, but you're pretty close.

You're already checking if the enemy can see you with the ray cast. However, if they can't, you're not starting a timer--which is what needs to happen. If they CAN see you, the timer needs to be set again to zero.

If the timer gets above whatever value you want, then that's when you can restart your enemy, so to speak.

Almost there, the hard part is done.

On a side note, to help clean your code up a bit, I would suggest changing your boolean checks from:

 if(chasing == true)

to

 if(chasing)

It's less code, means the same, and you'll find out quick that the less code you can write (usually) the more readable (and therefore maintainable) your code base is.

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 Julian_Spring · Mar 26, 2013 at 01:28 PM 0
Share

You are a sir

Thanks

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

11 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

Related Questions

i can't set destination, using AI Navmesh via raycast 1 Answer

why won't my code work? 1 Answer

Help with Enemy AI 1 Answer

2D sidescroller ranged character shooting problem. 0 Answers

Enemy following Player in range 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