Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Retreating AI using A* teleporting

Discussion in 'Navigation' started by arnaudloche, Jun 26, 2023.

  1. arnaudloche


    Jun 7, 2023
    So I'm making an AI using A* in 2D for an enemy that move toward the player when a certain distance away from it, stop when close and retreat when when even closer.
    The part moving toward and stopping part work as intended. The retreating part don't tho. It's supposed to set the target from the AIDestinationSetter to a transform who is child of the enemy. And set this transform to the negative of the player position relative to the enemy.
    Code (CSharp):
    1. using Pathfinding;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine
    6. public class AIEnemy : MonoBehaviour
    7. {
    8.         [SerializeField] AIDestinationSetter aIDestinationSetter;
    9.         [SerializeField] AIPath aIPath;
    10.         [SerializeField] GameObject player;
    11.         [SerializeField] Transform target;
    13.         public float reareatRange;
    15.         void Start()
    16.         {
    17.                target = gameObject.transform.GetChild(1);
    18.                player = GameObject.FindWithTag("Player");
    19.                aIDestinationSetter = GetComponent<AIDestinationSetter>();
    20.                aIPath = GetComponent<AIPath>();
    21.        }
    22.        void Update()
    23.        {
    24.       = target;
    26.                if (Vector2.Distance(player.transform.position, transform.position) < retreatRange)
    27.                {
    28.                SetStateRetreating(aIDestinationSetter);
    29.                }
    30.         }
    32.         void SetStateRetreating(AIDestinationSetter aIDestinationSetter)
    33.         {
    34.                 Vector2 playerPositionRelativeToEnemy = player.transform.position - transform.position;
    35.                 Vector2 newTarget = -playerPositionRelativeToEnemy;
    36.                target.position = newTarget;
    37.         }
    (Note: I have removed anything not related to the retreating code)

    I have found that this code seemingly set a good position for retreating. No problem in that regard. The problem is that my enemy teleport to this point rather than moving toward it using the pathfinder.
    I have tried bunch of different code and none of them worked...
    I thank you a lot already for any answer!

    PS: I am new to unity so if you find really dumb stuffs in my code don't be mad xD

    EDIT: Ok so I've found a way to make it work. I have implemented rotation so that the Enemy always face the player and Added a child transform behind the enemy that is set to be the target when retreating. I don't know why I haven't had this idea before. That being said, I would love to know why my original implementation didn't work.
    Last edited: Jun 27, 2023