Search Unity

Question Looking for ai path finding without using nav mesh agent, is there any tutorial/s ?

Discussion in 'Navigation' started by shamenraze1988, Dec 23, 2020.

  1. shamenraze1988

    shamenraze1988

    Joined:
    Nov 24, 2020
    Posts:
    208
    This script is attached to a transform and the transform is moving towards a target.

    Code (csharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using System.Linq;
    6. using UnityEngine;
    7. using UnityEngine.UI;
    8.  
    9. public class Follow : MonoBehaviour
    10. {
    11.     public Transform targetToFollow;
    12.     public Transform missionTarget;
    13.     public GameObject naviParent;
    14.     public Text textDistance;
    15.     public Text textSpeed;
    16.     public float lookAtRotationSpeed;
    17.     public float moveSpeed;
    18.     public float followRadius = 1.5f;
    19.     public float fastRadius = 5f;
    20.     public float speedBoost = 0.5f;
    21.  
    22.     private bool isNaviChild = false;
    23.  
    24.     void Start()
    25.     {
    26.        
    27.     }
    28.  
    29.     // Update is called once per frame
    30.     void FixedUpdate()
    31.     {
    32.         Vector3 lTargetDir = targetToFollow.position - transform.position;
    33.  
    34.         lTargetDir.y = 0.0f;
    35.  
    36.         transform.rotation = Quaternion.RotateTowards(transform.rotation,
    37.             Quaternion.LookRotation(lTargetDir), Time.time * lookAtRotationSpeed);
    38.  
    39.         float ms = moveSpeed;
    40.         var distance = Vector3.Distance(transform.position, targetToFollow.position);
    41.         // Compute a position no further than followRadius away from our target.
    42.         Vector3 fromTarget = Vector3.ClampMagnitude(
    43.             -lTargetDir, followRadius);
    44.         Vector3 stopPoint = targetToFollow.position + fromTarget;
    45.  
    46.         // Compute a speed that's faster when far away and slower when close.
    47.         float speedBlend = Mathf.Clamp01((distance - followRadius) / (fastRadius - followRadius));
    48.  
    49.         ms = moveSpeed + speedBlend * speedBoost;
    50.  
    51.         // Move as far as we can at our speed ms to reach the stopPoint, without overshooting.
    52.         transform.position = Vector3.MoveTowards(transform.position,
    53.             stopPoint, Time.deltaTime * ms);
    54.  
    55.         var dist = Vector3.Distance(transform.position, targetToFollow.position);
    56.         if(dist < 0.1f && isNaviChild == false)
    57.         {
    58.             transform.parent = targetToFollow;
    59.             transform.localPosition = new Vector3(0, 0, 0);
    60.             transform.localRotation = Quaternion.identity;
    61.             transform.localScale = new Vector3(0.001f, 0.001f, 0.001f);
    62.  
    63.             isNaviChild = true;
    64.         }
    65.     }
    66.  
    67.     private void Mission()
    68.     {
    69.  
    70.     }
    71. }
    72.  
    The problem will be if there is any obstacles and how to avoid them ?
    For example :

    Simple wall, How to find the shortest way ? Or if the wall sides are equal then how to decide what direction path to move ?

    Or if for example the player was walking around a cave or around a house and the transform by accident went inside the house or the cave how will the transform know to find the way back out and continue moving towards the player ?

    What about a rock that have some shapes ? Trees and other obstacles.

    The player has "forgotten" the transform the player doing his own stuff in the game and the transform should find a way and sometime somehow to reach the player. This is the main logic.


    The reason that I don't want to use nav mesh agent is because the nav mesh agent is more relevant to terrain/s and I want something to cover also the terrain area but also areas that are not terrain like just a simple plane with many obstacles.

    I want to start thinking of some logic ai path finding system. It's not have to be 100% accurate but it should do the work in most of the situations.