Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 JUnityer · Jul 05, 2011 at 04:36 PM · triggerplayeraienemylinecast

Patrolling AI doesn't see player if it's standing still

I have a problem with my patrolling AI script (again). I'm going to use it in my topdown Pac-Man style stealth game.

So my current enemy has a large box collider trigger component and the following script attached to it. The script moves the enemy around through waypoints. It also checks if the player enters the trigger and uses linecasting to tell if there are walls or anything blocking the view.

 var waypoint : Transform[];
 var speed : float = 10;
 var currentWaypoint : int = 0;
 var loop : boolean = true;
 var player : GameObject;
 private var character : CharacterController;
 private var inRange : boolean = false;


 function Start ()
 {
 character = GetComponent(CharacterController);
 }

 function OnTriggerStay(CollisionInfo : Collider)
 {
     if(CollisionInfo.gameObject.tag == "Player")
     {
         inRange = true;
     }
 }

 function Update () 
 {

     if(inRange == true)
     {
         var hit : RaycastHit;

         if(!Physics.Linecast(transform.position, player.transform.position,hit))
         {
     
         Debug.Log("I see you!");
         }
     
     }
 
 
     if(currentWaypoint < waypoint.length)
     {
         var target : Vector3 = waypoint[currentWaypoint].position;
         target.y = transform.position.y; // keep waypoint at character's height
         var moveDirection : Vector3 = target - transform.position;
         if(moveDirection.magnitude < 1)
         {
             transform.position = target; // force character to waypoint position
             currentWaypoint++;
         }
         else
         {
             transform.LookAt(target);
             character.Move(moveDirection.normalized * speed * Time.deltaTime);
         }
     }
     else
     {
         if(loop)
         {
             currentWaypoint=0;
         }
     }
 }

 function OnTriggerExit (CollisionInfo : Collider) 
 {
     if(CollisionInfo.gameObject.tag == "Player")
     {
         inRange = false;
     }
 }

This works very fine, but if I move my player into the enemy's patrolling route and stand still when it goes by, it doesn't notice me at all. But it does if it walks straight at me (so that our CharacterControllers collide).

Why is this happening? And how could I fix this? All answers and suggestions are welcome. I have no idea!

Thanks in advance!

Comment
Add comment · Show 1
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 aldonaletto · Jul 05, 2011 at 05:20 PM 0
Share

How are you moving the player? Is it a Character Controller?

1 Reply

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

Answer by sneftel · Jul 05, 2011 at 04:44 PM

Give your enemy a RigidBody and set it to kinematic. Otherwise its own motion won't invoke triggers.

Comment
Add comment · Show 6 · 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 JUnityer · Jul 05, 2011 at 04:50 PM 0
Share

Oh, I just managed to make my script use CharacterController.$$anonymous$$ove to move with ins$$anonymous$$d of Rigidbody.velocity yesterday. It was very glitchy that way. And making it kinematic would require many lines of script to prevent it from going through walls, wouldn't it? Any other ways to do this?

avatar image sneftel · Jul 05, 2011 at 05:00 PM 0
Share

None of what you said there is relevant. Having a kinematic RigidBody does not affect character control.

avatar image aldonaletto · Jul 05, 2011 at 05:01 PM 0
Share

Character Controllers are detected by triggers and colliders. You may have problems if your player is moved with Translate. Are you using Character.$$anonymous$$ove to move your player too?

avatar image JUnityer · Jul 05, 2011 at 05:29 PM 0
Share

Now I see what Ben meant! So the enemy can have BOTH Rigidbody AND a Character Controller. Sorry for that... Now it detects me even if I'm standing still, and works good. Thank you! And yes I use Character.$$anonymous$$ove in my player's script aswell.

avatar image sneftel · Jul 05, 2011 at 05:30 PM 1
Share

Read his question again -- in addition to a character controller, he also has a large box collider attached. That's the volume he wants to use for triggering.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Guides or Tutorials for a Very Basic Enemy AI? [Unity 5] 1 Answer

Enemy detection in the light(Javascript) 1 Answer

Can I make Ai using only a focus script and a bullet script? 1 Answer

Enemy Attraction/Aggro 1 Answer

How to make an AI like slender ? 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