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 straydogstrut · Apr 03, 2010 at 06:35 PM · raycastcollidertriggerlinecastsight

Enemy line of sight using linecast and colliders

Okay, i've got this almost working, I just need some help with the last part. I have a few NPCs in my game patrolling back and forth. Each has a cube mesh as a child with the renderer component turned off. Basically when ever the player character touches that mesh, I am resetting the player characters position (cruel I know!). I've also added a linecast to determine if there are any walls between the NPC and the player, and i've put both the player and the enemy on the Ignore Raycast layer. My code was:

var thePlayer : GameObject; var respawnPoint : Transform; private var canSeePlayer : boolean = false;

function OnTriggerEnter (CollisionInfo : Collider) {

if(CollisionInfo.gameObject.tag == "Player"){

 var hit : RaycastHit;

 if(Physics.Linecast(transform.position, thePlayer.transform.position, hit)){

     canSeePlayer = false;

 } else {
     canSeePlayer = true;
     thePlayer.transform.position = respawnPoint.position;
     }

 }

}

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

I realised that this was only performing the raycast once when the player touched the trigger box. This meant that the player could be behind a wall and unseen, but remain so even if the NPC moved to the player's side of the wall. To try to fix this I amended my code to use the trigger to determine if the player is within the NPC's view area (the box) and moved the linecast to the update function to repeatedly check if they are in line of sight:

var thePlayer : GameObject; var respawnPoint : Transform; private var inRange : boolean = false;

function OnTriggerEnter (CollisionInfo : Collider) {

 if(CollisionInfo.gameObject.tag == "Player"){

     inRange = true;

 } else {
     inRange = false;
     }

}

function Update(){

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

     if(!Physics.Linecast(transform.position, thePlayer.transform.position, hit)){

         thePlayer.transform.position = respawnPoint.position;
         inRange = false;
     }
 }

}

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

 }

}

Although this seems to work better, if the player character has been inRange but not in clear sight, the player still remains unseen by the NPC even after the NPC has moved into clear sight. The NPC walks through the player oblivious. Also, despite changing back to inRange = false in OnTriggerExit and when the player is replaced, it doesn't seem to change back from True after the first time it's set in OnTriggerEnter.

I have tried using OnTriggerStay, however this only replaces the PC just as the NPC finishes walking through him. I've also read about Physics.OverlapSphere but I believe this would allow the NPC to see in all directions?

I'd appreciate any help with this that anyone has the time to offer.

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 KvanteTore · Apr 03, 2010 at 07:53 PM

i think the problem is the inRange=false in your OnCollisionEnter. if the npc first is in range of the player, and then comes within range of another object, it forgets that it was in range of the player.

Try to remove the 'else' in OnCollisionEnter

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 straydogstrut · Apr 03, 2010 at 08:47 PM 0
Share

Hi $$anonymous$$vanteTore, you're absolutely right, I can't believe I didn't spot that! Works fine now, thank you=)

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

No one has followed this question yet.

Related Questions

Adding force relative to the distance from an object 1 Answer

Prevent shooting when gun is inside wall 1 Answer

Raycast hits a collider and halts. 1 Answer

Raycasts hit triggers disabled - force collision for specific raycast 1 Answer

raycast ignore trigger colliders? 5 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