Search Unity

Question Diversifying a Pursuing AI

Discussion in 'Scripting' started by coatline, Jul 29, 2022.

  1. coatline

    coatline

    Joined:
    Jul 31, 2019
    Posts:
    17
    Basically, I'm wondering how one would go about coding something that isn't:
    Code (CSharp):
    1. transform.MoveTowards(transform.position, player.transform.position, Time.deltaTime);
    for a simple pursuing enemy.

    I'm looking for an enemy that instead of bunching up behind the player in one spot, maybe uses trig functions to offset its position a little while still being smart enough to get to a stationary player.

    My best try is something like this:
    Code (CSharp):
    1. float targetX = playerTransform.position.x + Mathf.Sin(timeCreated + Time.time) * offsetAmount;
    2. float targetY = playerTransform.position.y + Mathf.Cos(timeCreated + Time.time) * offsetAmount;
    3.  
    4. transform.MoveTowards(transform.position, new Vector3(targetX, targetY), Time.deltaTime);
    But when I did this, the player could just stand still and the enemies would just circle the player.

    I've looked around the internet for something like this but to no avail. Any ideas are welcome.
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,448
    maybe could try:
    linecast to player, check if hit other enemies,
    then move sideways until nobody is in between this enemy and player.

    asset store has some ai formation plugins also, could check if anything interesting.
     
    coatline likes this.