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

find direction by navmesh system

Discussion in 'Navigation' started by afshin_a_1, Aug 12, 2022.

  1. afshin_a_1

    afshin_a_1

    Joined:
    Apr 26, 2015
    Posts:
    48
    hey guys.
    i simply want to get the direction of reaching to player by using unity navigation system.
    i don't want navmesh agent component to move the enemy. just get the direction. i have enemies with different types of movement.
    right now, it first finds the player position, then moves towards player straightforward even if there is an obstacle Infront of it. and it gets stuck behind it.
    Thanks in advance
    here is my enemy code:
    Code (CSharp):
    1.  
    2.     Rigidbody rgdBody;
    3.     Transform target;
    4.  
    5.     [SerializeField] float moveDelay = 1;
    6.     [SerializeField] float forceTowardsPlayer = 3;
    7.     [SerializeField] Vector3 force = new Vector3(3, 6, 3);
    8.     [SerializeField] Vector3 torque = new Vector3(2, 2, 2);
    9.  
    10.     Vector3 direction;
    11.  
    12.     void Start()
    13.     {
    14.         target = GameObject.FindObjectOfType<PlayerMovement>().transform;
    15.         MovementManager();
    16.     }
    17.  
    18.     void MovementManager()
    19.     {
    20.             direction = (target.position - transform.position).normalized;
    21.             rgdBody.AddForce(direction.x * forceTowardsPlayer, force.y, direction.z * forceTowardsPlayer, ForceMode.Impulse);
    22.  
    23.             rgdBody.AddTorque(new Vector3(torque.x, torque.y, torque.z), ForceMode.Impulse);
    24.  
    25.             if (moveDelay >= 0)
    26.                 Invoke("MovementManager", moveDelay);
    27.     }
    and here is unity package of an example with player and enemies(attached file)
     

    Attached Files:

    Last edited: Aug 19, 2022
    lightbeauty likes this.
  2. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    You need to phrase your question differently.

    You want to get the direction(location)? of your Target, thats a basic transform.postiion, if you want the turn rotation, a basic calculation is enough.

    If you want pathing similar to the NavMeshAgent but with the Rigidbody, then you will have to write your own logic of avoiding obstacles until the end.

    Basically Moving towards it and if there is an obstacle, move a bit to the left/right, in production will not be enough.

    Having a bigger Terrain, moving left or right. If an obstacle occur, will not always work since you can have bigger obstacles in the way leading to a totally different destination will cause problems
     
  3. lightbeauty

    lightbeauty

    Joined:
    Mar 6, 2019
    Posts:
    3
    I'm also looking for a solution to this issue. Any answers from the experts?
     
  4. afshin_a_1

    afshin_a_1

    Joined:
    Apr 26, 2015
    Posts:
    48
    I think you misunderstood my question. to clear it up for you and others: question is about "getting only the *direction* of *reaching to player* by helping from unity navigation system." that's it. it's not about movement, it's about direction. no matter if i'm moving my objects by unity translate or rigidboy or my own movemnt system. pls forget about rigidbodies or movement. just focus on a vector3 that holds direction.
    why helping from unity navigation system? because it creates path every moment. what is path? some points connected with lines until reaching the target. then my enemy's z axis aligns to the first line of path, if we use nav agent move, enemy moves towards it.
    let's stop here. i don't want nav mesh agent movement, only the direction is what i want. what direction? that point or points in path in reaching to player. just tell me how can i achieve the answer by any way you can think of.
    many thanks in advance.
     
    Last edited: Aug 19, 2022
  5. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
  6. MarekUnity

    MarekUnity

    Unity Technologies

    Joined:
    Jan 6, 2017
    Posts:
    179
    Just as @Terraya mentioned, you can use the NavMeshAgent.CalculatePath API, or if you don't want to use NavMeshAgent at all, there is a static API that has the same function - NavMesh.CalculatePath.
     
    afshin_a_1 likes this.
  7. afshin_a_1

    afshin_a_1

    Joined:
    Apr 26, 2015
    Posts:
    48
    MarekUnity likes this.