- Home /
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 nothing
} else {
Debug.Log("Not looking");
}
}
Here is the actual error I get:
Now, obviously I do have a renderer attached to my cam object, it is the main camera. This is driving me up the walls, any ideas?
If you don't know how, watch the tutorial video on the right
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 hit: RaycastHit;
if (Physics.Linecast(angel.position, target.position, hit) && hit.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 hit: RaycastHit;
if (Physics.Linecast(angel.position, target.position, hit) && hit.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 hit: RaycastHit;
if (Physics.Linecast(angel.position, target.position, hit) && hit.transform == target){
// there's a clear line of sight between angel and target
}
:
Ok thanks for the explanation. The script won't be any different if its the player trying to see the angel will it?
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
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.
Answer by Benproductions1 · Aug 14, 2013 at 12:31 AM
http://lmgtfy.com?q=Unity+renderer
Then you would know that a camera != a renderer and you would have not asked the question :)
Ok, changed the cam to graphics, that worked, but it only returns one bool value... is there another way for continuous detection?
What do you mean by continuous? It's reevaluated every frame
Your answer
Follow this Question
Related Questions
Graphics raycasting if renderer in game object is seen and not occluded 1 Answer
Changing two different objects renderer colour 1 Answer
Renderer.isVisible always returning false 2 Answers
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