Hey guys!
I have a little problem with my Gunscript in a Top Down Shooter.
Look at the GIF to see my Problem ![]()

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