Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to make an autoaim for a 2D game

Discussion in 'Scripting' started by mfarisammar, Jun 8, 2020.

  1. mfarisammar

    mfarisammar

    Joined:
    Mar 30, 2020
    Posts:
    6
    Hello there, thought i might ask here since no one answering in Unity Answer. So i try to develop a script that suppose to allow player to lock into the nearest enemy(yes it change when another closer enemy is presence) and able to instantiate bullet from a GameObject. My problem here is i cant make the GameObject to look at the enemy. So anyone can help ?

    Code (CSharp):
    1.  using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class AutoWeaponAim : MonoBehaviour
    5. {
    6.      private Transform target;
    7.      public float range = 15f;
    8.      public Transform shotPoint;
    9.      public Transform partToRotate;
    10.      public float offset = -90;
    11.      //public string enemyTag = "Enemy" ;
    12.      void Start()
    13.      {
    14.          InvokeRepeating("UpdateTarget", 0f, 0.5f);
    15.      }
    16.      void UpdateTarget()
    17.      {
    18.          GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
    19.          float shortestDistance = Mathf.Infinity;
    20.          GameObject nearestEnemy = null;
    21.          foreach (GameObject enemy in enemies)
    22.          {
    23.              float distanceToEnemy = Vector3.Distance (transform.position, enemy.transform.position);
    24.              if (distanceToEnemy < shortestDistance)
    25.              {
    26.                  shortestDistance = distanceToEnemy;
    27.                  nearestEnemy = enemy;
    28.              }
    29.          }
    30.          if (nearestEnemy != null && shortestDistance <= range)
    31.          {
    32.              target = nearestEnemy.transform;
    33.          }
    34.          else
    35.          {
    36.              target = null;
    37.          }
    38.          Vector3 dir = target.position - transform.position;
    39.          Quaternion lookRotation = Quaternion.LookRotation(dir);
    40.          Vector3 rotation = lookRotation.eulerAngles;
    41.          partToRotate.rotation = Quaternion.Euler(0f , rotation.z , rotation.z);
    42.          Debug.Log("Part 1");
    43.          //partToRotate.LookAt(new Vector3(0f, 0f, (partToRotate.position.z )));
    44.          Debug.DrawLine (this.transform.position, nearestEnemy.transform.position);
    45.      }
    46.      void Update()
    47.      {
    48.          if(target == null)
    49.          {
    50.              return;
    51.          }
    52.          else
    53.          {
    54.               Vector3 dir = target.position - transform.position;
    55.          Quaternion lookRotation = Quaternion.LookRotation(dir);
    56.          Vector3 rotation = lookRotation.eulerAngles;
    57.          //partToRotate.rotation = Quaternion.Euler(rotation.y , rotation.z , rotation.z);
    58.          partToRotate.LookAt(new Vector3(0f, 0f, (partToRotate.position.z + offset)));
    59.          Debug.Log("part 2");
    60.        
    61.          //Debug.DrawLine (this.transform.position, nearestEnemy.transform.position);
    62.        
    63.          }
    64.      }
    65.        
    66.    
    67.      void OnDrawGizmosSelected()
    68.      {
    69.          Gizmos.color = Color.red;
    70.          Gizmos.DrawWireSphere(shotPoint.position, range);
    71.      }
    72. }
    73.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    You can use transform.LookAt() to aim the +Z axis of a transform anywhere you like.

    If you are in 2D you probably want to aim a base invisible transform that is turned 90 degrees to the right (or left) so that it matches your visible object's facing. The visible object would be a child of the transform you rotate.

    There is a LOT going on above. Try paring it down: start a fresh tiny script, put two visible objects in the scene, and work on hardcoding one object to look at the other and get all the sprites right. That way you have a working example you can reason about while you fix your main game screen.
     
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    If you don't want to mess with 3D stuff, it's only a few lines of code to look at a point in 2D space using atan2. (There's an example and everything, though note the example uses the XZ plane rather than XY.)
     
  4. mfarisammar

    mfarisammar

    Joined:
    Mar 30, 2020
    Posts:
    6
    Thank you guys for your answers and advices. I succeed getting what i want based on your infos. Thanks a lot dudes :D. Yall just make my day better.
     
    Kurt-Dekker likes this.