Search Unity

AI Movement

Discussion in 'Scripting' started by BatgirlDee, Oct 10, 2019.

  1. BatgirlDee

    BatgirlDee

    Joined:
    Mar 20, 2019
    Posts:
    36
    hi there i have a script which works pretty much how want to to except it would be nice if i could get the ai to ether move to the rear of the player at all points of go in the direction the player is facing

    here is the code so far
    any help would be great by the way its a 2d game

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlaerAI : MonoBehaviour
    4. {
    5.     public Transform playerPos;
    6.     public GameObject Player;
    7.     public float speed;
    8.  
    9.     public float mobRange;
    10.  
    11.  
    12.     Animator animator;
    13.     bool isMoving;
    14.  
    15.     void Start()
    16.     {
    17.         Player = GameObject.FindGameObjectWithTag("Player");
    18.         animator = GetComponent<Animator>();
    19.     }
    20.  
    21.     private void Update()
    22.     {
    23.         TotDistance();
    24.     }
    25.     void TotDistance()
    26.     {
    27.         var testX = transform.position.x;
    28.         var testY = transform.position.y;
    29.  
    30.         var pTestX = Player.transform.position.x;
    31.         var pTestY = Player.transform.position.y;
    32.  
    33.         var xDistance = testX - pTestX;
    34.         var yDistance = testY - pTestY;
    35.  
    36.         var totDistance = Mathf.Pow(xDistance, 2) + Mathf.Pow(yDistance, 2);
    37.         totDistance = Mathf.Sqrt(totDistance);
    38.  
    39.  
    40.         Debug.Log("Total Distance: " + totDistance);
    41.  
    42.         FacePlayer(xDistance, totDistance);
    43.  
    44.  
    45.     }
    46.  
    47.     void FacePlayer(float xDistunce, float tutDistunce)
    48.     {
    49.         if (xDistunce < -0.5)
    50.         {
    51.             transform.localScale = new Vector3(2, 2, 1);
    52.             if (tutDistunce > mobRange)
    53.             {
    54.                 transform.Translate(speed * Time.deltaTime, 0, 0, Space.World);
    55.                 animator.SetBool("isMoving", isMoving = true);
    56.             }
    57.             else
    58.             {
    59.                 animator.SetBool("isMoving", isMoving = false);
    60.             }
    61.  
    62.         }
    63.         if (xDistunce > 0.5)
    64.         {
    65.             transform.localScale = new Vector3(-2, 2, 1);
    66.             if (tutDistunce > mobRange)
    67.             {
    68.                 transform.Translate(-speed * Time.deltaTime, 0, 0, Space.World);
    69.                 animator.SetBool("isMoving", isMoving = true);
    70.             }
    71.             else
    72.             {
    73.                 animator.SetBool("isMoving", isMoving = false);
    74.             }
    75.         }
    76.     }
    77. }
     
  2. BatgirlDee

    BatgirlDee

    Joined:
    Mar 20, 2019
    Posts:
    36
    so is there any help out there for the above please
     
  3. BatgirlDee

    BatgirlDee

    Joined:
    Mar 20, 2019
    Posts:
    36
    hello there is the any help with the above
     
  4. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    You could check player's forward direction (the direction that is your forward movement direction) and then use this to set a point behind the player at the desired distance, like 2 units behind the player and move your enemies toward that point?

    You can find examples how to do this if you do some googling.
     
  5. BatgirlDee

    BatgirlDee

    Joined:
    Mar 20, 2019
    Posts:
    36
    I'm working on a work around as the ai has a rigidbody2d which is need for parts of the game and also trying to get it to jump over things as when needed
     
  6. BatgirlDee

    BatgirlDee

    Joined:
    Mar 20, 2019
    Posts:
    36
    here is my work around but the OnTriggerOnEnter is not working

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerAI : MonoBehaviour
    4. {
    5.    
    6. public Transform playerPos;
    7.     public GameObject Player;
    8.     public float speed;
    9.  
    10.     public float mobRange;
    11.  
    12.     Collider2D lift;
    13.  
    14.     GameObject AI;
    15.  
    16.     Rigidbody2D Rigid;
    17.  
    18.     Animator animator;
    19.     bool isMoving;
    20.  
    21.     void Start()
    22.     {
    23.         Player = GameObject.FindGameObjectWithTag("Player");
    24.         animator = GetComponent<Animator>();
    25.         // Rigid = GetComponent<Rigidbody2D>();
    26.         Destroy(GetComponent<Rigidbody2D>());
    27.         lift = GetComponent<BoxCollider2D>();
    28.         lift.enabled = false;
    29.     }
    30.  
    31.     public void Update()
    32.     {
    33.         TotDistance();
    34.  
    35.     }
    36.     void TotDistance()
    37.     {
    38.         var testX = transform.position.x;
    39.         var testY = transform.position.y;
    40.  
    41.         var pTestX = Player.transform.position.x;
    42.         var pTestY = Player.transform.position.y;
    43.  
    44.         var xDistance = testX - pTestX;
    45.         var yDistance = testY - pTestY;
    46.  
    47.         var totDistance = Mathf.Pow(xDistance, 2) + Mathf.Pow(yDistance, 2);
    48.         totDistance = Mathf.Sqrt(totDistance);
    49.  
    50.  
    51.         Debug.Log("Total Distance: " + totDistance);
    52.  
    53.         FacePlayer(xDistance, totDistance);
    54.  
    55.     }
    56.  
    57.     void FacePlayer(float xDistunce, float tutDistunce)
    58.     {
    59.         if (xDistunce < -0.5)
    60.         {
    61.             transform.localScale = new Vector3(2, 2, 1);
    62.             if (tutDistunce > mobRange)
    63.             {
    64.                 transform.Translate(speed * Time.deltaTime, 0, 0, Space.World);
    65.                 animator.SetBool("isMoving", isMoving = true);
    66.             }
    67.             else
    68.             {
    69.                 animator.SetBool("isMoving", isMoving = false);
    70.             }
    71.  
    72.         }
    73.         if (xDistunce > -0.5)
    74.         {
    75.             transform.localScale = new Vector3(-2, 2, 1);
    76.             if (tutDistunce > mobRange)
    77.             {
    78.                 transform.Translate(-speed * Time.deltaTime, 0, 0, Space.World);
    79.                 animator.SetBool("isMoving", isMoving = true);
    80.             }
    81.             else
    82.             {
    83.                 animator.SetBool("isMoving", isMoving = false);
    84.             }
    85.         }
    86.  
    87.     }
    88.  
    89.   public void OnTriggerEnter2D(Collider2D other)
    90.     {
    91.         if (other.gameObject.CompareTag("Lift"))
    92.         {
    93.             Debug.Log("hit Lift 1");
    94.             lift.enabled = true;
    95.         }
    96.         if (other.gameObject.CompareTag("lift2"))
    97.         {
    98.             Debug.Log("hit Lift 2");
    99.             Rigid = AI.AddComponent<Rigidbody2D>();
    100.         }
    101.     }
    102.  
    also keep getting this error

    NullReferenceException: Object reference not set to an instance of an object
    PlayerAI.OnTriggerEnter2D (UnityEngine.Collider2D other)
     
    Last edited: Oct 17, 2019