Enemy Bullets travel in an arc

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.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;


public class EnemyPistol : MonoBehaviour
{
  



    public float offset;


    public AudioSource Shoot;
    public AudioSource ReloadSFX;

    public GameObject EnemyProjectile;
    public Transform shotPoint;

    public Transform player;

    private float shotCooldown;
    public float startTimeForCooldown;


    void start()
    {
        player = GameObject.FindGameObjectWithTag("Player").transform;
        shotCooldown = startTimeForCooldown;
    }

    void Update()
    {
        if (shotCooldown <= 0)
        {
            Instantiate(EnemyProjectile, shotPoint.position, Quaternion.identity);
            shotCooldown = startTimeForCooldown;
        }
        else
        {
            shotCooldown -= Time.deltaTime;
        }
      
      

    }
}

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?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyProjectile : MonoBehaviour
{

    public float speed;
    public float lifeTime;
    public float distance;
    public int damage;

   public Transform player;
    private Vector2 target;


    public LayerMask whatIsSolid;

    public GameObject destroyEffect;

    void Start()
    {
        Invoke("DestroyProjectile", lifeTime);

        player = GameObject.FindGameObjectWithTag("Player").transform;
        target = new Vector2(player.position.x, player.position.y);
    }

    void Update()
    {
        transform.position = Vector2.MoveTowards (transform.position, target, speed * Time.deltaTime);
       
        if(transform.position.x == target.x && transform.position.y == target.y)
        {
            DestroyProjectile();
        }
        RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, transform.up, distance, whatIsSolid);
        if (hitInfo.collider != null)
        {
            if (hitInfo.collider.CompareTag("Player"))
            {
                hitInfo.collider.GetComponent<Player>().TakeBulDamage();

            }

            if (hitInfo.collider.CompareTag("Ground"))
            {
                hitInfo.collider.GetComponent<GroundSFX>().PlaySound = true;

            }


            DestroyProjectile();

        }


        transform.Translate(Vector2.up * speed * Time.deltaTime);
    }

    void DestroyProjectile()
    {
        Instantiate(destroyEffect, transform.position, Quaternion.identity);
        Destroy(gameObject);
    }
}

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.

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.

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.

1 Like

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.

Then you need just regular shooting

Thank you so much!

after trying to modify this script for my game as it is 2D I couldn’t get it to work.

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.

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.

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:

heh funny the ranged combat video is the one that I used i found it quite useful