Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved I have problems with the lookAt

Discussion in '2D' started by Gabo19x, Jan 4, 2021.

  1. Gabo19x

    Gabo19x

    Joined:
    Oct 23, 2020
    Posts:
    99
    Hello, I am a new.


    In the script I'm using a LookAt but apparently it doesn't work. The context of the script is that when the player approaches a "chase zone" the idea is that the enemy looks at the player, but does not.

    Code (CSharp):
    1. public class IABasica : MonoBehaviour
    2. {
    3.     public Animator animacion;
    4.     public float velocidad = 0f;
    5.     private float tiempo;
    6.     private int i = 0;
    7.     private Vector2 pos_actual;
    8.     public Transform[] puntos;
    9.  
    10.     public float zonaPersecucion, zonaAtaque;
    11.     public GameObject jugador;
    12.     public Transform jugadorTransform;
    13.     private float d;
    14.  
    15.     public float daño = 0f;
    16.     private float v;
    17.  
    18.     void Start()
    19.     {
    20.         tiempo = Random.Range(0,3);
    21.         jugador = GameObject.FindGameObjectWithTag("Jugador");
    22.     }
    23.  
    24.     void Update()
    25.     {
    26.         d = Vector3.Distance(jugador.transform.position, transform.position);
    27.  
    28.         if(d > zonaPersecucion)
    29.         {
    30.             transform.position = Vector2.MoveTowards(transform.position, puntos[i].transform.position, velocidad * Time.deltaTime);
    31.             Vector3 mirar = transform.TransformDirection(puntos[i].transform.position - transform.position);
    32.  
    33.             if(Vector2.Distance(transform.position, puntos[i].transform.position) < 0.1f)
    34.             {
    35.                 if(tiempo <= 0)
    36.                 {
    37.                     if(puntos[i] != puntos[puntos.Length-1])
    38.                     {
    39.                         i++;
    40.                     }
    41.                     else
    42.                     {
    43.                         i = 0;
    44.                     }
    45.                     tiempo = Random.Range(0,3);
    46.                 }
    47.             }
    48.             else
    49.             {
    50.                 tiempo -= Time.deltaTime;
    51.             }
    52.         }
    53.         else if(d > zonaAtaque && d <= zonaPersecucion)
    54.         {
    55.             Vector3 objetivo = jugador.transform.position;
    56.             transform.position = Vector3.MoveTowards(transform.position, objetivo, velocidad*Time.deltaTime);
    57.             transform.LookAt(objetivo);
    58.         }
    59.         else if(d <= zonaAtaque)
    60.         {
    61.             animacion.SetTrigger("Ataque");
    62.         }
    63.  
    64.        
    65.     }
    66.  
    67.     void OnDrawGizmos()
    68.     {
    69.         Gizmos.color = Color.red;
    70.         Gizmos.DrawWireSphere(transform.position, zonaPersecucion);
    71.         Gizmos.DrawWireSphere(transform.position, zonaAtaque);
    72.     }
    73. }
    Or maybe you don't quite get it right, thanks for any help...
     
  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    For 2D LookAt doesn't work. The idea behind LookAt is that it sets the forward direction of an object which is how 3D works, forward is forward. In 2D forward has no context when the z coordinate isn't really used. For 2D it is right that is forward. So you need to do a little bit of math to get the angle and set the z rotation to that angle.

    Code (CSharp):
    1. Vector direction = targetPosition - myPosition;//Get the direction and distance.
    2. float dist = direction.magnitude;//Get the distance first
    3. direction.Normalize();//Turn it into an actual direction vector.
    4. float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;//Atan2 gives the angle in radians, Unity works in degrees.
    5.  
    6. transform.rotation = Quaternion.Euler(0f, 0f, angle);
     
    aelsingel likes this.
  3. Gabo19x

    Gabo19x

    Joined:
    Oct 23, 2020
    Posts:
    99
    Code (CSharp):
    1. public class IABasica : MonoBehaviour
    2. {
    3.     public Animator animacion;
    4.     public float velocidad = 0f;
    5.     private float tiempo;
    6.     private int i = 0;
    7.     private Vector2 pos_actual;
    8.     public Transform[] puntos;
    9.  
    10.     public float zonaPersecucion, zonaAtaque;
    11.     public GameObject jugador;
    12.     public Transform jugadorTransform;
    13.     private float d;
    14.  
    15.     public float daño = 0f;
    16.     private float v;
    17.  
    18.     void Start()
    19.     {
    20.         tiempo = Random.Range(0,3);
    21.         jugador = GameObject.FindGameObjectWithTag("Jugador");
    22.     }
    23.  
    24.     void Update()
    25.     {
    26.         d = Vector3.Distance(jugador.transform.position, transform.position);
    27.  
    28.         if(d > zonaPersecucion)
    29.         {
    30.             transform.position = Vector2.MoveTowards(transform.position, puntos[i].transform.position, velocidad * Time.deltaTime);
    31.             Vector3 mirar = transform.TransformDirection(puntos[i].transform.position - transform.position);
    32.  
    33.             if(Vector2.Distance(transform.position, puntos[i].transform.position) < 0.1f)
    34.             {
    35.                 if(tiempo <= 0)
    36.                 {
    37.                     if(puntos[i] != puntos[puntos.Length-1])
    38.                     {
    39.                         i++;
    40.                     }
    41.                     else
    42.                     {
    43.                         i = 0;
    44.                     }
    45.                     tiempo = Random.Range(0,3);
    46.                 }
    47.             }
    48.             else
    49.             {
    50.                 tiempo -= Time.deltaTime;
    51.             }
    52.         }
    53.         else if(d > zonaAtaque && d <= zonaPersecucion)
    54.         {
    55.             Vector3 objetivo = jugador.transform.position;
    56.             transform.position = Vector3.MoveTowards(transform.position, objetivo, velocidad*Time.deltaTime);
    57.  
    58.             //-----------------------------------------------
    59.         }
    60.         else if(d <= zonaAtaque)
    61.         {
    62.             //animacion.SetTrigger("Ataque");
    63.         }
    64.  
    65.         Vector3 direccion = jugadorTransform.position - transform.position;
    66.         float dist = direccion.magnitude;
    67.         direccion.Normalize();
    68.         float angulo = Mathf.Atan2(direccion.y, direccion.x) * Mathf.Rad2Deg;
    69.         Debug.Log(angulo);
    70.         transform.rotation = Quaternion.Euler(0f, 0f, angulo);
    71.     }
    72.  
    73.     void OnDrawGizmos()
    74.     {
    75.         Gizmos.color = Color.red;
    76.         Gizmos.DrawWireSphere(transform.position, zonaPersecucion);
    77.         Gizmos.DrawWireSphere(transform.position, zonaAtaque);
    78.     }
    79. }
    80.  

    In the script, it is in the update and should always look at the player no matter where they go.
    In line 1 of your code is the Vector Vector2 or Vector3?
    Also, the variable "dist" is not used, why?
     
    Last edited: Jan 5, 2021
  4. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    My code was an example of how to do it, not to replace or just add to your code. I don't know what your code is doing fully due to variable names being in a different language.

    Doesn't matter but they both need to be either Vector2 or Vector3.

    It was meant to show you that you can get the distance using this math so you can avoid using Vector2/3.Distance which does the same thing only you don't get a direction. It would essentially replace d in your code.
     
  5. Gabo19x

    Gabo19x

    Joined:
    Oct 23, 2020
    Posts:
    99
    Fix it a bit and now it is turning, but it is not doing it properly, (in the image you can see that it is inside the chase zone) you are looking at it from the side, what is this?
     

    Attached Files:

  6. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    When the character is not rotated which way does he face? If he is not facing right you need to offset the rotation so he is facing the correct way. I tend to make everything face to the right by default so I don't need an offset to the rotation. 0 degrees is right, so if he is facing up be default, it should only need a -90 degree offset.

    Code (CSharp):
    1. transform.rotation = Quaternion.Euler(0f, 0f, angulo - 90f);
    I might be wrong on the offset, so don't quote me on it I don't use them often enough.
     
  7. payalzariya07

    payalzariya07

    Joined:
    Oct 5, 2018
    Posts:
    85
  8. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    Not the camera but the calculation will use left as up instead, no camera is involved.

    I have not tested this myself as I almost always want the direction and distance anyway, but it should work.
     
  9. Gabo19x

    Gabo19x

    Joined:
    Oct 23, 2020
    Posts:
    99
    I think the problem is where the eyes are, that is, what is the point where it rotates and how it sees, I don't know if it makes me understand...
     

    Attached Files:

  10. Gabo19x

    Gabo19x

    Joined:
    Oct 23, 2020
    Posts:
    99
    Another thing the lookAt (target, Vector3.left) "destroys" the object, it distorts it...