Search Unity

2D enemy turning

Discussion in '2D' started by Tidvis_Martin, Aug 3, 2019.

  1. Tidvis_Martin

    Tidvis_Martin

    Joined:
    Mar 12, 2019
    Posts:
    24
    Hello, Unity forum!

    I am currently working on a 2D game where we need enemy AI's to be able to move and "look" in different directions and I was wondering if you could help me out with some of the code for it.

    This is what I have for code in my enemy movement behavior so far, it's probably really crude but it's just what I am able to do at my current skill level:


    Code (CSharp):
    1. public class EnemyMovement : MonoBehaviour
    2. {
    3.     public Enemy enemy;
    4.  
    5.     public bool patrolingEnemy = false;
    6.     public float ppRadius = 1.0f;
    7.     public GameObject[] patrolPoints;
    8.     [HideInInspector]
    9.     public GameObject target;
    10.     [HideInInspector]
    11.     public GameObject player;
    12.     private int currentIndex = 0;
    13.  
    14.     [Range(0, 5)]
    15.     public float movementspeed;
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         if (patrolingEnemy == true && player == null)
    21.         {
    22.             //Enemy movement.
    23.             if (Vector3.Distance(patrolPoints[currentIndex].transform.localPosition, transform.localPosition) < ppRadius)
    24.             {
    25.                 currentIndex++;
    26.  
    27.                 if (currentIndex >= patrolPoints.Length)
    28.                 {
    29.                     currentIndex = 0;
    30.                 }
    31.             }
    32.  
    33.             target = patrolPoints[currentIndex];
    34.             //Add enemy turn here to look at next patrol point here
    35.         }
    36.         else if (player != null)
    37.         {
    38.             target = player;
    39.             //Add enemy turn to look at player here.
    40.         }
    41.         else
    42.         {
    43.             target = gameObject;
    44.         }
    45.  
    46.         transform.localPosition = Vector3.MoveTowards(transform.localPosition, target.transform.localPosition, Time.deltaTime * movementspeed);
    47.      
    48.     }
    49.  
    50.     void OnTriggerEnter2D(Collider2D collision)
    51.     {
    52.         if (collision.tag == "Player")
    53.         {
    54.             player = collision.gameObject;
    55.         }
    56.     }
    57.  
    58.     void OnTriggerExit2D(Collider2D collision)
    59.     {
    60.         player = null;
    61.     }
    62. }
    So I've tried a few different snippets people have suggested online but I can't seem to get any of them to work. Some of the ones I have tried are:

    Code (CSharp):
    1. transform.right = target.transform.localPosition - transform.localPosition;
    And

    Code (CSharp):
    1.          Vector3 diff = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
    2.          diff.Normalize();
    3.          float rot_z = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
    4.          transform.rotation = Quaternion.Euler(0f, 0f, rot_z - 90);
    As well as some variation of these.

    I guess what I am really after is just some piece of code that can help me rotate the Y-axis of the object 180 degrees depending on which direction the enemy is looking since they aren't meant to be able to rotate in the z axis or the x-axis.

    Thanks for the read-through.
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Just a note here:
    Local position is the Transform's position relative to the parent transform. This math will only work if both the target and the transform have the same parent, or both have no parent (in which case Local and World will be the same).

    If you change that to
    target.transform.position - transform.position
    it is comparing world position which will work in any hierarchy.

    I notice in your code snippets you're exclusively using localPosition, so make sure you understand the difference because I think you want to be using "position" instead.
     
    vakabaka likes this.