Gunshooting Top Down

Hey guys!

I have a little problem with my Gunscript in a Top Down Shooter.
Look at the GIF to see my Problem :smile:

My Problems are the different bullet speeds depending on the distance between spawn and cursor and the “backshooting”

Weapon Script

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

public class Weapon : MonoBehaviour {

    public enum WeaponType { oneHanded, twoHanded, DualHanded }
    public WeaponType weaponType = WeaponType.oneHanded;

    public enum FireMode { Automatic, Manual}
    public FireMode fireMode = FireMode.Automatic;


    public SetTorsoSprite Torso;
  
    public float fireSpeed;

    public Rigidbody2D bullet;
    public float bulletSpeed;

    public GameObject muzzleFlash;
    public Transform muzzlePos;


    public Camera mainCam;
    public float offset;

    public bool canShoot;
  
    void Start () {
        Torso.SetSprite((int)weaponType);
    }

    void Update()
    {
        if (fireMode == FireMode.Automatic)
        {
            if (Input.GetButton("Fire1"))
            {
                if (canShoot)
                {
                    StartCoroutine(Fire());
                }
            }
        }
        else if (fireMode == FireMode.Manual)
        {
            if (Input.GetButtonDown("Fire1"))
            {
                if (canShoot)
                {
                    StartCoroutine(Fire());
                }
            }
        }
    }
    IEnumerator Fire()
    {
        Vector3 mousePos = Input.mousePosition;
        mousePos.z = 5.23f;
        Vector3 objectPos = Camera.main.WorldToScreenPoint(transform.position);
        mousePos.x = mousePos.x - objectPos.x;
        mousePos.y = mousePos.y - objectPos.y;
        float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
        GameObject muzzle = Instantiate(muzzleFlash, muzzlePos.position, Quaternion.Euler(new Vector3(0, 0, angle )));
        Rigidbody2D bPrefab = Instantiate(bullet, muzzlePos.position, Quaternion.Euler(new Vector3(0, 0, angle + offset))) as Rigidbody2D;
        bPrefab.AddForce(mousePos * bulletSpeed);
        canShoot = false;
        yield return new WaitForSeconds(fireSpeed);
        canShoot = true;
        StopCoroutine(Fire());
    }
}

Thanks in Advance !
Greetings xTeare

rubs crystal ball so… the bullets going at different speeds? (just the gif on it’s own gives us no concept of what you are trying to achieve… words and a picture would be much better)

you want to make the vector you are using as a direction for the bullet a “unit” vector first so that’s it magnitude doesn’t affect the result.

https://docs.unity3d.com/ScriptReference/Vector3-normalized.html
https://docs.unity3d.com/ScriptReference/Vector3.Normalize.html

Hi,
yes i mean different speeds based on the distance between spawn and cursor. Also the Backshooting

EDIT:
Just solved myself.
Instead of using the Cursor Position i just used my Players direction (transform.up) to add Force

1 Like