Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How to use Raycast to move the player around the Navmesh.

Discussion in 'Navigation' started by Nosmer, Aug 15, 2023.

  1. Nosmer

    Nosmer

    Joined:
    Oct 26, 2020
    Posts:
    6
    So lets say I have a tower with multiple floors. The navmesh is built without any impassable sections, and I use this code to navigate the player
    Code (CSharp):
    1. Ray ray = mainCamera.ScreenPointToRay(Mouse.current.position.ReadValue());
    2.         RaycastHit hit;
    3.  
    4.         if (Physics.Raycast(ray, out hit, 200, movementMask))
    5.         {
    6.             characterAgent.SetDestination(hit.point);
    7.         }
    with movementMask set to "Walkable" and both floors having this mask applied.
    However, if I try to navigate the player to the higher floor when its positioned on top of another, somehow it gets ignored and the player always navigates to the floor below. Could anyone explaing to me me why is this happening and how do I make the player navigate to the top floor? Sorry if its the wrong forum section for this kind of question.
     
  2. ChrisKurhan

    ChrisKurhan

    Joined:
    Dec 28, 2015
    Posts:
    265
    It sounds like you are using movementMask as a Navigation Layer mask instead of a Physics Layer Mask. If your have your "floor" set to "Default" layer, try to set "movementMask" to "Default" and see if it works better for you.

    If you are using NavMesh.Raycast() you should use movementMask as a Navigation Layer, but for Physics you need to be sure to be using Physics Layers.