• 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 secondry2 · Aug 14, 2013 at 12:29 AM · camerarenderernoattached

There is no renderer attacked to main camera

Hi!

ok, so im trying to detect if a gameobject is outside the view of the main camera of the character controller. Here is my script, it is attached to the enemy:

 var angel : Transform;
 var cam : GameObject;
 var target : Transform;
 
 function Start () {
 
 }
 
 function Update () {
     var rot = target.position - angel.position;
     var dist = Vector3.Distance(angel.position, target.position);
     rot.y = 0;
     
     if (cam.renderer.isVisible) {
         //do not$$anonymous$$ng
     } else {
         Debug.Log("Not looking");
     }
 }

Here is the actual error I get:

alt text

Now, obviously I do have a renderer attached to my cam object, it is the main camera. T$$anonymous$$s is driving me up the walls, any ideas?

Comment
Add comment · Show 4
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 Benproductions1 · Aug 14, 2013 at 12:29 AM 0
Share

Please format your code

avatar image secondry2 · Aug 14, 2013 at 12:31 AM 0
Share

What do you mean?

avatar image Benproductions1 · Aug 14, 2013 at 12:34 AM 0
Share

If you don't know how, watch the tutorial video on the right

avatar image RDC · Jun 17, 2014 at 06:01 AM 0
Share

I am still not able to resolve this problem though.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by aldonaletto · Aug 14, 2013 at 12:55 AM

You're using isVisible wrong: you should check the target's isVisible property instead of the camera's (even because the camera usually has no renderer...)

But renderer.isVisible actually only tells whether the object is inside the viewing angle of any camera, and will be true even if it's completely obscured by dozens of other objects, mountains etc. If you want to know whether the angel can see the target, the test should be different:

   var $$anonymous$$t: RaycastHit;
   if (Physics.Linecast(angel.position, target.position, $$anonymous$$t) && $$anonymous$$t.transform == target){
      // there's a clear line of sight between angel and target
   }

If you have only one camera and it's attached to the angel, you could also use isVisible in order to make sure the target is inside the angel viewing angle:

 if (target.renderer.isVisible){ // if target inside viewing angle...
   var $$anonymous$$t: RaycastHit;
   if (Physics.Linecast(angel.position, target.position, $$anonymous$$t) && $$anonymous$$t.transform == target){
      // there's a clear line of sight between angel and target
   }
 }

If the enemy doesn't have a camera, or if there's more than one, check the angle between the enemy's forward direction and the target direction instead:

 var dir = target.position - angel.position;
 if (Vector3.Angle(dir, angel.forward) < 60){ // visibility is 60 degrees in any direction
   var $$anonymous$$t: RaycastHit;
   if (Physics.Linecast(angel.position, target.position, $$anonymous$$t) && $$anonymous$$t.transform == target){
      // there's a clear line of sight between angel and target
   }
 :      

 
Comment
Add comment · Show 4 · 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 secondry2 · Aug 14, 2013 at 01:04 AM 0
Share

Ok thanks for the explanation. The script won't be any different if its the player trying to see the angel will it?

avatar image Benproductions1 · Aug 14, 2013 at 01:08 AM 0
Share

The problem with this approach is that you have to have colliders on anything that can be in-front of the "angel" and it only tests to the middle of the "angel", meaning if there is a tiny block, between the centre of the angel and the camera, your test will note "see it". Considering that, this is still the best method XD

avatar image aldonaletto · Aug 14, 2013 at 01:41 AM 0
Share

Usually the visibility test is something like this:

 var dir = target.position - transform.position;
 if (Vector3.Angle(dir, transform.forward) < viewAngle){ // visibility is viewAngle in any direction
   var hit: RaycastHit;
   if (Physics.Linecast(transform.position, target.position, hit) && hit.transform == target){
      // there's a clear line of sight between this object and target
   }
 }    

Attach this to the player, to the enemy or to any object that you want to perform the test.

NOTE: @Benproductions1 is right: the target must have a collider, and only the line between the object and the target pivots is taken into account.

avatar image secondry2 · Aug 14, 2013 at 01:44 AM 0
Share

Thanks for your help both of you!

avatar image
0

Answer by Benproductions1 · Aug 14, 2013 at 12:31 AM

http://lmgtfy.com?q=Unity+renderer

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 secondry2 · Aug 14, 2013 at 12:33 AM 0
Share

I know what a renderer is...

avatar image Benproductions1 · Aug 14, 2013 at 12:34 AM 0
Share

Then you would know that a camera != a renderer and you would have not asked the question :)

avatar image secondry2 · Aug 14, 2013 at 12:38 AM 0
Share

So, ok, Ill see what I can do then

avatar image secondry2 · Aug 14, 2013 at 12:42 AM 0
Share

Ok, changed the cam to graphics, that worked, but it only returns one bool value... is there another way for continuous detection?

avatar image Benproductions1 · Aug 14, 2013 at 12:44 AM 0
Share

What do you mean by continuous? It's reevaluated every frame

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 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

Related Questions

Renderer.isVisible always returning false 2 Answers

Graphics raycasting if renderer in game object is seen and not occluded 1 Answer

Duplicate everything in a set space in all directions? 2 Answers

How can i active again a render texture after set it to null ? 1 Answer

How Do I Not Render Objects That My Player Doesn't See 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