Apr 8

I’ve implemented a RayCast:ing version of finding out if a UsableObject is in front of the player. This is done by casting a ray from the players position 1 meter forward. If it hits anything on the layer Usable, it knows an object is on front.

The old version was by having a trigger collider that sensed if the player entered its area.

Differences positive or negative?
RayCast: When turning around the door will not be sensed, because the player won’t “see” it.
Old: Opposite. While the player is in the trigger area it will be sensed.

The Raycast method does seem a bit more natural, but it also involved casting a ray every frame. Not sure if that is cpu intensive since I do not have a profiler (no pro version).

Try out the difference here. The left door is the new one. Raycast version

PS I just noticed something seems weird with this web version. Can’t change any settings or look up with the mouse. Will investigate later.


4 comments so far...

  • MrM Said on April 8th, 2013 at 18:24:

    I’d use both. First check if the player is in the right area, then if he is looking at the thing.

    This way you can turn off raycasting on every frame except when standing in certain hotspots, and you’ll avoid “sneaky usage” by for example looking at a switch from a weird angle (while behind bars or something) and still be able to pull the switch even though he shouldn’t be able to reach it. If you use an area you know he is in the right place and isn’t cheating.

    A good thing about raycasting is that you can for example place two switches close to each other and have the user able to easily decide which one to pull.

    The right door doesn’t seem to be working in the web version either btw. Also the mouse still isn’t grabbed by the embedded game it seems.

    I reconsidered what I said before btw, about implementing an enemy. First things first: You should make the player able to pick up objects and drop them using E. No more awkward pushing of the cube to the button! Not to mention it’ll be very useful in puzzles later on.

  • freso Said on April 9th, 2013 at 16:24:

    You know what? That is a good point. I hadn’t thought about the cheating aspect.

    Of course, I don’t have to execute it every frame, could do it 5 times per second or so and probably be pretty responsive. Anyway, I will do both checks. Good thinking! 🙂

    Btw, if you want to die, you can just jump out into the abyss.

  • freso Said on April 9th, 2013 at 18:34:

    Unfortunately I now have a problem with the Raycasting method. I can’t know what kind of UsableObject I encounter, so I can’t get the component easily because it wil be named UsableDoor for example. Perhaps I can loop through all components and if it is of type UsableObject, I use that, but seems like a bad workaround.
    Sigh. 🙁

  • freso Said on April 9th, 2013 at 21:56:

    Ignore that, I got it to work. 😀