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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question I'm having difficulties making the player aim with a controller

Discussion in '2D' started by EagleEyeDevelopments, Jul 15, 2020.

  1. EagleEyeDevelopments

    EagleEyeDevelopments

    Joined:
    Jan 10, 2019
    Posts:
    2
    So I have set up a jump and move input functions but the player aiming is hard. I've spent more than an hour trying to solve this but I can't. I'm trying to make the player aim automatically at the enemy since I was having difficulties making it so he could free aim.
    The crosshair works and stays on the enemy but the gun function doesn't work somehow, any help would be appreciated.

    Code (CSharp):
    1. public class PointAndShoot : MonoBehaviour
    2. {
    3.     public GameObject crosshairs;
    4.     public GameObject player; //this is the gun of the player
    5.     public GameObject bulletPrefab;
    6.     public GameObject bulletStart;
    7.  
    8.     private Transform enemy;
    9.  
    10.     private float timeBtwShots;
    11.     public float startTimeBtwShots;
    12.  
    13.     public GameObject muzzleFire;
    14.  
    15.     public float bulletSpeed = 20.0f;
    16.  
    17.     private Vector3 target;
    18.     private Transform TARGET;
    19.     public GameObject[] TTarget;
    20.     public float range = 10f;
    21.  
    22.     float nextTimeToSearch = 0;
    23.  
    24.     [Header("Audio")]
    25.     public AudioClip pistolSound;
    26.  
    27.     void Start()
    28.     {
    29.         Cursor.visible = false;
    30.  
    31.         timeBtwShots = startTimeBtwShots;
    32.  
    33.         InvokeRepeating("UpdateTarget", 0f, 0.01f);
    34.  
    35.         enemy = GameObject.FindGameObjectWithTag("Enemy").transform;
    36.     }
    37.  
    38.  
    39.     void UpdateTarget()
    40.     {
    41.         GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
    42.         float shortestDistance = Mathf.Infinity;
    43.         GameObject nearestEnemy = null;
    44.  
    45.         target = transform.GetComponent<Camera>().ScreenToWorldPoint(new Vector2(crosshairs.transform.position.x, crosshairs.transform.position.y));
    46.         TTarget = enemies;
    47.  
    48.         crosshairs.transform.position = new Vector2(gameObject.transform.position.x, gameObject.transform.position.y);
    49.  
    50.         foreach (GameObject enemy in enemies)
    51.         {
    52.             float distanceToEnemy = Vector3.Distance(transform.position, enemy.transform.position);
    53.             if (distanceToEnemy < shortestDistance)
    54.             {
    55.                 shortestDistance = distanceToEnemy;
    56.                 nearestEnemy = enemy;
    57.  
    58.                 crosshairs.transform.position = nearestEnemy.transform.position;
    59.             }
    60.         }
    61.  
    62.         if(nearestEnemy != null && shortestDistance <= range)
    63.         {
    64.             TARGET = nearestEnemy.transform;
    65.         }
    66.         else
    67.         {
    68.             TARGET = null;
    69.         }
    70.     }
    71.  
    72.  
    73.     public void Update()
    74.     {
    75.         if (TARGET == null)
    76.             return;
    77.  
    78.  
    79.  
    80.         Vector3 difference = target - player.transform.position;
    81.         float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
    82.         player.transform.rotation = Quaternion.Euler(0.0f, 0.0f, rotationZ);
    83.  
    84.         Vector3 aimLocalScale = Vector3.one;
    85.         if (rotationZ > 90 || rotationZ < -90)
    86.         {
    87.             aimLocalScale.y = -1f;
    88.         }
    89.         else
    90.         {
    91.             aimLocalScale.y = +1f;
    92.         }
    93.         player.transform.localScale = aimLocalScale;
    94.  
    95.         float fire = Input.GetAxis("Fire1");
    96.  
    97.         if (timeBtwShots <= 0)
    98.         {
    99.             if (fire != 0f)
    100.             {
    101.                 float distance = difference.magnitude;
    102.                 Vector2 direction = difference / distance;
    103.                 direction.Normalize();
    104.                 fireBullet(direction, rotationZ);
    105.  
    106.                 timeBtwShots = startTimeBtwShots;
    107.  
    108.                 AudioSource audio = GetComponent<AudioSource>();
    109.                 audio.PlayOneShot(pistolSound);
    110.             }
    111.  
    112.         }
    113.         else
    114.         {
    115.             timeBtwShots -= Time.deltaTime;
    116.         }
    117.  
    118.         if (target == null)
    119.         {
    120.             Debug.Log("FindPlayer");
    121.             FindPlayer();
    122.             return;
    123.         }
    124.  
    125.     }
    126.  
    127.     public void fireBullet(Vector2 direction, float rotationZ)
    128.     {
    129.         GameObject b = Instantiate(bulletPrefab) as GameObject;
    130.         b.transform.position = bulletStart.transform.position;
    131.         b.transform.rotation = Quaternion.Euler(0.0f, 0.0f, rotationZ);
    132.         b.GetComponent<Rigidbody2D>().velocity = direction * bulletSpeed;
    133.         PlayerBullet pBullet = b.GetComponent<PlayerBullet>();
    134.  
    135.         if (pBullet != null)
    136.             pBullet.Seek(TARGET);
    137.     }
    138.  
    139.     void FindPlayer()
    140.     {
    141.         if (nextTimeToSearch <= Time.time)
    142.         {
    143.             GameObject searchResult = GameObject.FindGameObjectWithTag("Player");
    144.             if (searchResult != null)
    145.                 target = searchResult.transform.position;
    146.             nextTimeToSearch = Time.time + 0.5f;
    147.         }
    148.     }
    149.  
    150.     void OnDrawGizmosSelected()
    151.     {
    152.         Gizmos.color = Color.red;
    153.         Gizmos.DrawWireSphere(transform.position, range);
    154.     }
    155.  
    156. }
    This is the gun code, I'm not sure i need to also put the player controller script here so I'll just post the gun one.
    Thanks
     
  2. EagleEyeDevelopments

    EagleEyeDevelopments

    Joined:
    Jan 10, 2019
    Posts:
    2
    Perhaps it would be easier to just make him aim freely with the controller stick? I tried that but it didn't turn out so well, got some bugs.