Is object visible to player?

How can i check wether or not an object is visible to the player?

A few cases:
Object is infront of you → true
Object is behind you → false
Object is infront of you, but behind a wall → false
Object is on front of you, behind a wall but a small part (for example a hand) is not covered by the wall → true

This would be very useful for, let’s see… A powerup, that enables some kind of aimbot for a given amount of time. Otherwise it would just shoot at the walls.

Or for triggering some behaviour in enemies once they get found

Well my first thought would be to calculate the angle of the object to the camera. If within a certain angle then you consider it potentially within view. Then raycast from several points on the object to the camera. If any raycast gets all the way without hitting anything else then the object is visible to the camera.

Doing this on a bunch of objects would be expensive, so I’d limit it to some interval, like once per second or so instead of once per frame.

This is just off the top of my head. Someone else may have solved this in their game and has a more informed opinion.

1 Like

The information necessary to determine this is probably already calculated somewhere in the process of drawing the frame (those pixels get there somehow), but I don’t know if you can access it in any practical fashion.

Otherwise, as Joe says, you could first calculate whether the object appears within the camera’s field of view, and then (if so) raycast to several points on the object to determine whether those points are obstructed or not.

You might be able to use some kind of weird hack where you use a second camera that only renders certain layers and compare the frame generated by that camera to the frame generated by the main camera, but I’m not sure what would buy you anything.

1 Like

You are right, this would work. But as you said this would be very compute intensive + very inaccurate.

I mean, there’s got to be some way considering some hackers are pulling off aimbots of without accessing the games code.

I think i have come up with a solution… Maybei could just create a 3D polygon collider (mesh collider with custom collider mesh), so that it resembles roughly the shape of the enemy, put it on a child object of the enemy and raycast from every vertex of the collider mesh towards the player using a for loop. If any ray would hit the player, the object would very likely be visible

I was imagining like 6 rays for something like a human character (1 head, 1 body, 1 for each hand and foot). Raycasting from every vertex of a mesh collider is probably going to be a lot more rays.

1 Like

Yes, of course! Very good idea.
In this case, i would just create a child object called ,ray probes" or something like that.
This object would contain a script that casts a raycast from every transform of it’s children.
Much like a skeletal rig.

Hierachy:

Enemy
    RayProbes
        LHand
        RHand
        Head
        Body
        LKNee
        RKnee
1 Like

If you wanted to make an aimbot that runs “on top” of a game, without interacting with the game’s code at all, then you’d only have access to the screen image, and you’d need to do some kind of image recognition to detect the objects you are looking for. That’s certainly possible–computer vision has gotten fairly impressive in recent years–but it’s going to be computationally expensive, and I doubt it would recognize small parts of a person (like a hand) unless they were extremely visually distinctive.

I don’t have much experience with aimbots, but I suspect the large majority of them do not do that. They probably “peek” at the game’s internal state, either by hacking the game client or by snooping on network traffic, so that they have access to a list of all the objects in the game and the mathematical coordinates of where they are located (without needing to infer that data from the pixels on your computer screen). That seems like it would be easier and more effective, and I see little reason for them not to do it.

But since you’re writing your own game, you have access to all of that information, too.

There’s absolutely no question you can do this by raycasting to a few well-chosen points on the target’s body; it’s just a question of optimizing how many rays and how often you cast them to get a good speed/accuracy trade-off.

But it’s possible there are also even better ways of doing it. (I just couldn’t tell you definitively what they are.)

Raycasting against child collider after the various culling system is pretty much the standard way of doing, I nhave never met any other magic way to do it (at least for generic arbitrary level geometry).

You can add camera to character’s head and check if object is occluded or not.