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 TurboHermit · Feb 17, 2014 at 04:47 PM · raycastingrendererline of sight

Enable renderer when in line of sight

Hi, I'm currently trying to pull off enabling a renderer of an object when it's in line of sight of the object. I wouldn't really know how to pull this off. I've tried Raycasting which still seems like the most plausible salution, but I can't get it to cast in a 360 radius. Does anyone encountered the same problem before?

Example: Left: Blue square is the player, enemy currently not visible. Right: Enemy is now visible.

alt text

untitled-1.jpg (17.4 kB)
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

2 Replies

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

Answer by whydoidoit · Feb 17, 2014 at 04:52 PM

You shouldn't need to cast 360 - just cast from the player's position to the enemy's position and see if you get there (hit the enemy, not something else first).

Comment
Add comment · Show 8 · 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 whydoidoit · Feb 17, 2014 at 04:54 PM 1
Share

If you want to limit it so that the player is actually pointing in the right direction, get the vector to the enemy:

    var vectorToEnemy = (enemy.transform.position - player.transform.position);

And check if the angle between it and the player's forward is within range:

   if(Vector3.Angle(vectorToEnemy.normalized, transform.forward) < 80) {
       Enemy is visible
   }
avatar image whydoidoit · Feb 17, 2014 at 04:57 PM 1
Share

Yes that's it. Work out a range if necessary, then apply the basic "in the viewing area" check as described in the comment, then cast a ray and find out what it hits. If it's the enemy then you can see it.

avatar image whydoidoit · Feb 17, 2014 at 04:59 PM 1
Share
      RaycastHit hit;
      if(Physics.Raycast(transform.position, vectorToEnemy, out hit, vectorToEnemy.magnitude)) {
             if(hit.collider.gameObject == enemy.gameObject) {
                 //I can see you
             }
      }
avatar image whydoidoit · Feb 17, 2014 at 05:00 PM 1
Share

I often do it the other way around to avoid the loop - so each enemy casts towards the player if they are in range and the player is facing them. It's a matter of choice though.

avatar image whydoidoit · Feb 17, 2014 at 05:10 PM 1
Share

You are using a raycast, the second parameter is not a position, it's a vector from the start of the ray. You can use Linecast if you want to use two positions - but I was going the raycast route so you could check for viewing angle, when you need the vector anyway.

Show more comments
avatar image
0

Answer by NickP_2 · Feb 17, 2014 at 04:56 PM

Use a raycast as following:

  • orgin: one of the 2 squares, the square u didnt chose shall be named "othersquare". Now the direction will be: othersquare.transform.position - transform.position.

Now just do a Physics.raycast check, if thr raycast returns anothet object than the othersquare, disable the renderer!

Ps: sorry i coulnd write the code im on my train back home, typing on my phone

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

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

I want to paint the surface of a mesh depending on if it has been seen by a camera, can this be done with shaders? 0 Answers

Changing two different objects renderer colour 1 Answer

[Line of Sight] Please evaluate my approach 1 Answer

Having problems changing the material of current target. 2 Answers

How to set alpha for object "hidden" by another? 0 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