Search Unity

Need To Set Agent To Point In Front Of Player

Discussion in 'Navigation' started by Corva-Nocta, Apr 13, 2019.

  1. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    461
    I'm working on a click to move game that is going rather well, but I want to add in a dodge system. I want the player to move X units left/right/forward but I want to do it using the navmesh. Basically I just need to set the agent to move to a point in front of the player, but I'm really not sure how I would do that.

    Can I say something like:
    Agent.SetDestination(transform.position + vector3.forward(10, 0, 0));

    Or do I have to do something more robust than that? I suppose I could have 4 empty game objects that are parented to my player and are X units away from him in all directions and just raycast down from them to get a point to move to. But that feels needlessly complicated.

    Any help would be great!
     
  2. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    300
    Can you provide more of a step-by-step explanation what you want to do here? Depending on what you want you might need different solutions.
    How I understand it you want:
    1) click on terrain to have an agent beginning to move.
    2) at any point during the movement, if the player presses a button:
    - dodge in a direction (depending on the button pressed?)
    - continue moving to the clicked destination
    Is that right, or do you want something different?
     
  3. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    600
    I'd suggest you use NavMesh.SamplePosition around where you want your agent to go. For approach would only work if your ground is perfectly flat and has no edges.