How do I check what's in front of the player using Raycasting?

  • Hey so I'm wanting to know how I can detect if a sprite is in front of him. The sprite in question is a door. I want my player to walk up to the door and be prompted to press E to open. I was wondering how i could do this with raycasting. So cast an invisible line that when it collides with a wall or the door, it sets a variable that says what the cast is overlapping.

    I am making a top down survival shooter.

    Thank you :)

    Tagged:

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • considering the player doesnt have to exactly face the door but just be walking close to it to trigger "open door', the simpliest way would be to add a layer and use block sprites as collision and make this layer invisible to the player (by selecting the layer and uncheck Initially visible in its propery).

    you would of course make the block sprite size bigger than the door.

    Then you will write the code where:

    - if player/sprite overlap the block sprite on your invisible layer

    >>> trigger the door animation

    >>> disable the solid state of the door that he can walk through it.

    you will have to reverse it (animation and solid state) when the player is not overlaping anymore the the block sprite that you use as collision detection if you want the door to close automatically and become solid again.

    sincdee you certainly will have more than one door ;) I would suggest to add an instance variable for the doors and block sprites you use for collision that when a block sprite is overlaped by the sprite of your player, you can grab the ID of the block and know exactly which door to open/close since youm made sure they share the same ID.

  • Thank you for your reply, it does make more sense to do it this way :)

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)