Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to tackle Navigation

Discussion in 'Navigation' started by Sigmundrr, Sep 24, 2019.

  1. Sigmundrr

    Sigmundrr

    Joined:
    Aug 17, 2019
    Posts:
    10
    Hey, i'm trying at the moment do dive deeper into the Topic Navigation. I work just for learning Expiriencereasons on an RPG Framework. Combatwise i will tackle a Grid&Turnbased solution.

    Now for general navigation (PC&NPCs) what do you think is a poroper Pathfinding solution? I had the feeling, the Unity Navmeshsolution is not the ''best'' in all cases, i had some Problems with it. But this could also be just bad usage of it, by me.

    second, maybe should i study how astar works and try this out?

    I also did the Unity Adventure Tutorial some time ago, this one works with the Navmesh, but combinied with the Eventsystem/eventtriggers. And also does some smoothing on the moving of the Agent.

    this is the code from the Tutorial:

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.AI;
    4. using UnityEngine.EventSystems;
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     public Animator animator;
    8.     public NavMeshAgent agent;
    9.     public float turnSmoothing = 15f;
    10.     public float speedDampTime = 0.1f;
    11.     public float slowingSpeed = 0.175f;
    12.     public float turnSpeedThreshold = 0.5f;
    13.     public float inputHoldDelay = 0.5f;
    14.    
    15.     private Interactable currentInteractable;
    16.     private Vector3 destinationPosition;
    17.     private bool handleInput = true;
    18.     private WaitForSeconds inputHoldWait;
    19.     private readonly int hashSpeedPara = Animator.StringToHash("Speed");
    20.     private readonly int hashLocomotionTag = Animator.StringToHash("Locomotion");
    21.     public const string startingPositionKey = "starting position";
    22.     private const float stopDistanceProportion = 0.1f;
    23.     private const float navMeshSampleDistance = 4f;
    24.     private void Start()
    25.     {
    26.         agent.updateRotation = false;
    27.         inputHoldWait = new WaitForSeconds (inputHoldDelay);
    28.         destinationPosition = transform.position;
    29.     }
    30.     private void OnAnimatorMove()
    31.     {
    32.         agent.velocity = animator.deltaPosition / Time.deltaTime;
    33.     }
    34.     private void Update()
    35.     {
    36.         if (agent.pathPending)
    37.             return;
    38.         float speed = agent.desiredVelocity.magnitude;
    39.        
    40.         if (agent.remainingDistance <= agent.stoppingDistance * stopDistanceProportion)
    41.             Stopping (out speed);
    42.         else if (agent.remainingDistance <= agent.stoppingDistance)
    43.             Slowing(out speed, agent.remainingDistance);
    44.         else if (speed > turnSpeedThreshold)
    45.             Moving ();
    46.        
    47.         animator.SetFloat(hashSpeedPara, speed, speedDampTime, Time.deltaTime);
    48.     }
    49.     private void Stopping (out float speed)
    50.     {
    51.         agent.Stop();
    52.         transform.position = destinationPosition;
    53.         speed = 0f;
    54.         if (currentInteractable)
    55.         {
    56.             transform.rotation = currentInteractable.interactionLocation.rotation;
    57.             currentInteractable.Interact();
    58.             currentInteractable = null;
    59.             StartCoroutine (WaitForInteraction ());
    60.         }
    61.     }
    62.     private void Slowing (out float speed, float distanceToDestination)
    63.     {
    64.         agent.Stop();
    65.         float proportionalDistance = 1f - distanceToDestination / agent.stoppingDistance;
    66.         Quaternion targetRotation = currentInteractable ? currentInteractable.interactionLocation.rotation : transform.rotation;
    67.         transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, proportionalDistance);
    68.         transform.position = Vector3.MoveTowards(transform.position, destinationPosition, slowingSpeed * Time.deltaTime);
    69.         speed = Mathf.Lerp(slowingSpeed, 0f, proportionalDistance);
    70.     }
    71.     private void Moving ()
    72.     {
    73.         Quaternion targetRotation = Quaternion.LookRotation(agent.desiredVelocity);
    74.         transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, turnSmoothing * Time.deltaTime);
    75.     }
    76.     public void OnGroundClick(BaseEventData data)
    77.     {
    78.         if(!handleInput)
    79.             return;
    80.        
    81.         currentInteractable = null;
    82.         PointerEventData pData = (PointerEventData)data;
    83.         NavMeshHit hit;
    84.         if (NavMesh.SamplePosition (pData.pointerCurrentRaycast.worldPosition, out hit, navMeshSampleDistance, NavMesh.AllAreas))
    85.             destinationPosition = hit.position;
    86.         else
    87.             destinationPosition = pData.pointerCurrentRaycast.worldPosition;
    88.         agent.SetDestination(destinationPosition);
    89.         agent.Resume ();
    90.     }
    91.     public void OnInteractableClick(Interactable interactable)
    92.     {
    93.         if(!handleInput)
    94.             return;
    95.         currentInteractable = interactable;
    96.         destinationPosition = currentInteractable.interactionLocation.position;
    97.         agent.SetDestination(destinationPosition);
    98.         agent.Resume ();
    99.     }
    100.     private IEnumerator WaitForInteraction ()
    101.     {
    102.         handleInput = false;
    103.         yield return inputHoldWait;
    104.         while (animator.GetCurrentAnimatorStateInfo (0).tagHash != hashLocomotionTag)
    105.         {
    106.             yield return null;
    107.         }
    108.         handleInput = true;
    109.     }
    110. }
     
  2. duncanmix

    duncanmix

    Joined:
    Sep 18, 2019
    Posts:
    1
    Hi,
    Do you know if it is possible to locate other people, track their movement in real time and see it?
     
  3. Sigmundrr

    Sigmundrr

    Joined:
    Aug 17, 2019
    Posts:
    10
    Yes, check out Sebastians Tutorial:


    But you should also be able to do that with the built in NavMesh and drawed Raycasts