I’m having an issue with a simple stand-in model I created. It is supposed to detect the player through raycasting.
The problem is that it’s local z axis is facing down, so when using transform.forward it only triggers when the player is beneath the model, rather than in front of it. Using Vector3.forward won’t work either.
I’m wondering if there is some way to reorient an objects local axes.
Another solution would be some way to prevent trigger colliders from detecting the player when they are behind an object (thus avoiding raycasting alltogether), though I don’t see how that would be possible.
Which way the axes point is up to you, so that shouldn’t be a problem. Or was the problem that the model was imported incorrectly in local space? (I’m guessing that’s the case.)
Vector3.forward is just the world Z axis (0,0,1), so yeah, that’s not going to help you much.
Sure, just reorient the object. I suspect you mean something else though (probably related to the model’s local-space orientation). Anyway, it sounds like you’ve got that part of things sorted.
You could use triggers and then ignore detected objects that are behind the player. That won’t address the occlusion issue though.
Accurately detecting whether one object is wholly or partially visible to another in a complex scene isn’t necessarily trivial, and the solution to this problem will often be context-dependent. The most straightforward solution though would probably be a field-of-view check to see if the player is within the entity’s FOV, followed by a raycast to determine (roughly) if the player is occluded relative to the entity. Are you looking for more accuracy than that? What do you mean by ‘too narrow’?
What I mean by too narrow is that I have a light source parented to the ‘enemy’ and I want the player to be detected when they enter the light.
Currently, however, the player can walk all around the light and only be detected by the ray when in the dead center.
As I said before, trigger colliders will ignore obstacles in between the two.
If you have the time, I would appreciate an example of how to use this field of view code, or at least a link to one (the unity script references never seem to have good concise example code).
Yeah, you’re not going to find an example in the documentation for something like this (it’s far too specific).
It sounds to me like what you want is a ‘cone’ or ‘wedge’ check to see if the player is roughly within the area illuminated by the light, and then a raycast to determine if the player is in the enemy’s line of sight.
For the ‘light’ test, there are different ways you could do it. One would be to create a convex model whose shape roughly corresponds to the area illuminated by the light, attach it to the enemy, make it a trigger, and then consider the player illuminated when the player is in the trigger. (Yes, this won’t account for line of sight, but that’s what the raycast is for.)
Another solution would be to measure the unsigned angle between the light direction vector and the vector from the light origin to the player’s position; if it’s less than the ‘spotlight angle’ for the light, the player is illuminated.