Search Unity

Question Best way for detecting player in Maze ? (thinking with Performance)

Discussion in 'Physics' started by ALittleBB, Jun 22, 2022.

  1. ALittleBB

    ALittleBB

    Joined:
    May 14, 2018
    Posts:
    4
    Hi, I would like some advice about detecting with Raycast or Collider

    Let's me explain my problem:

    My game is a Maze and has an object that with do an action when detecting a player in its area
    First I used Physic.Raycast to check 4 directions (Forward, Back, Left, and Right) with target Laymark (Player and Wall) It works as I want it to be.

    But I do read that Physic Raycast have some cost, and I used it in void.Update(), It does affect some performance (I think), but I'm improving it with Time checker like: Time.time - lastCallTime >= 0.2f less call as much as I can

    Then I think of Collider, It's easy to do and detect but the problem is Collider will detect the player even player is staying other side of a wall.




    Hope this picture will clearly more than my explain

    I'm very want to know there are other ways to do if Physic.Rasycast will be my last choice.

    Thank you.
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,512
    Easy. Simply throw a single raycast from the object to the player. If the ray collides with a wall or the distance is beyond some value, then the player is not detected. Otherwise, it's detected.

    Physics.Raycast is perfectly fine. No matter what you read out there, there's no "one rule fits them all" in terms of physics. Literally *everything* in physics has some cost. Simply learn how each physics feature works and use them conveniently for your project.