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

Enemy Bullets travel in an arc

Discussion in 'Scripting' started by stefansalewski91, Mar 27, 2020.

  1. stefansalewski91

    stefansalewski91

    Joined:
    Nov 17, 2019
    Posts:
    7
    I have a few scripts that are supposed to make the enemy shoot witch they do but not in the way I expected. The bullets travel in an arc instead of directly at the player here are the scripts:

    this is the weapon the enemy uses witch instantiates the bullet.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6.  
    7.  
    8. public class EnemyPistol : MonoBehaviour
    9. {
    10.  
    11.  
    12.  
    13.  
    14.     public float offset;
    15.  
    16.  
    17.     public AudioSource Shoot;
    18.     public AudioSource ReloadSFX;
    19.  
    20.     public GameObject EnemyProjectile;
    21.     public Transform shotPoint;
    22.  
    23.     public Transform player;
    24.  
    25.     private float shotCooldown;
    26.     public float startTimeForCooldown;
    27.  
    28.  
    29.     void start()
    30.     {
    31.         player = GameObject.FindGameObjectWithTag("Player").transform;
    32.         shotCooldown = startTimeForCooldown;
    33.     }
    34.  
    35.     void Update()
    36.     {
    37.         if (shotCooldown <= 0)
    38.         {
    39.             Instantiate(EnemyProjectile, shotPoint.position, Quaternion.identity);
    40.             shotCooldown = startTimeForCooldown;
    41.         }
    42.         else
    43.         {
    44.             shotCooldown -= Time.deltaTime;
    45.         }
    46.      
    47.      
    48.  
    49.     }
    50. }
    51.  
    This script handles the projectile and is supposed to make it move to the player's last position, instead, it travels in an arc and freezes once it gets above the player and it doesn't trigger the DestroyProjectile(); function. Can anyone help?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemyProjectile : MonoBehaviour
    6. {
    7.  
    8.     public float speed;
    9.     public float lifeTime;
    10.     public float distance;
    11.     public int damage;
    12.  
    13.    public Transform player;
    14.     private Vector2 target;
    15.  
    16.  
    17.     public LayerMask whatIsSolid;
    18.  
    19.     public GameObject destroyEffect;
    20.  
    21.     void Start()
    22.     {
    23.         Invoke("DestroyProjectile", lifeTime);
    24.  
    25.         player = GameObject.FindGameObjectWithTag("Player").transform;
    26.         target = new Vector2(player.position.x, player.position.y);
    27.     }
    28.  
    29.     void Update()
    30.     {
    31.         transform.position = Vector2.MoveTowards (transform.position, target, speed * Time.deltaTime);
    32.        
    33.         if(transform.position.x == target.x && transform.position.y == target.y)
    34.         {
    35.             DestroyProjectile();
    36.         }
    37.         RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, transform.up, distance, whatIsSolid);
    38.         if (hitInfo.collider != null)
    39.         {
    40.             if (hitInfo.collider.CompareTag("Player"))
    41.             {
    42.                 hitInfo.collider.GetComponent<Player>().TakeBulDamage();
    43.  
    44.             }
    45.  
    46.             if (hitInfo.collider.CompareTag("Ground"))
    47.             {
    48.                 hitInfo.collider.GetComponent<GroundSFX>().PlaySound = true;
    49.  
    50.             }
    51.  
    52.  
    53.             DestroyProjectile();
    54.  
    55.         }
    56.  
    57.  
    58.         transform.Translate(Vector2.up * speed * Time.deltaTime);
    59.     }
    60.  
    61.     void DestroyProjectile()
    62.     {
    63.         Instantiate(destroyEffect, transform.position, Quaternion.identity);
    64.         Destroy(gameObject);
    65.     }
    66. }
     
  2. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    488
    This is moving the bullet towards the player (it works like a tracking misle)
    transform.position = Vector2.MoveTowards (transform.position, target, speed * Time.deltaTime);


    This is moving the button up:
    transform.Translate(Vector2.up * speed * Time.deltaTime);


    Why would you want either of these? First one doesn't create a realistic bullet that goes forward, it instead creates a bullet that will literally follow the player wherever he goes. The second one just moves the bullet straight up.

    To prevent the bullet from getting stuck in the air above the player, remove the second version.
    To create a more realistic bullet look into Rigidbody.velocity to set the bullet's velocity to its forward axis or into raycasts to fire "lasers" onto players.
     
  3. stefansalewski91

    stefansalewski91

    Joined:
    Nov 17, 2019
    Posts:
    7
    ok, so I would get the player's last position and then use rigidbody.velocity instead of what I'm currently doing? thanks for the help.
     
  4. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    488
    It depends on your game. If your player can actually aim, then his target's position doesn't influence the trajectory of the bullet. Instead, when you spawn the bullet, you use its local axis to determine the direction it should move in. Try looking up shooting in Unity. There should be a ton of material on this topic.
     
    Joe-Censored likes this.
  5. stefansalewski91

    stefansalewski91

    Joined:
    Nov 17, 2019
    Posts:
    7
    in my game the player has movement and 3 different weapons, the player and enemies can both die and the player's bullets are shot to the mouse's position, they travel past the mouse cursor as well.
     
  6. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    488
    Then you need just regular shooting

     
  7. stefansalewski91

    stefansalewski91

    Joined:
    Nov 17, 2019
    Posts:
    7
    Thank you so much!
     
  8. stefansalewski91

    stefansalewski91

    Joined:
    Nov 17, 2019
    Posts:
    7
    after trying to modify this script for my game as it is 2D I couldn't get it to work.
     
  9. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    488
    Google mate. The only way you're going to get through these problems is if you research stuff for yourself. No one will hold your hand here.
     
  10. stefansalewski91

    stefansalewski91

    Joined:
    Nov 17, 2019
    Posts:
    7
    2 things this forum has been a last resort as I've spent all of yesterday googling and watching videos and a few minutes ago unity crashed and corrupted my game. I appreciate the help tho.
     
  11. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    488
    I used to be stuck at an issue for weeks or months, it's normal.

    This might help with your 2D game:


    Also, once you're ready, try looking into Git to keep backups of your project. I know that I've lost so much progress in the past because I didn't know how to use it, now I can't live without it. Brackeys covered it in a video a while back:
     
  12. stefansalewski91

    stefansalewski91

    Joined:
    Nov 17, 2019
    Posts:
    7
    heh funny the ranged combat video is the one that I used i found it quite useful