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. Dismiss Notice

Gunshooting Top Down

Discussion in 'Scripting' started by DarkToadster, Nov 20, 2016.

  1. DarkToadster

    DarkToadster

    Joined:
    Sep 10, 2015
    Posts:
    18
    Hey guys!

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

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


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Weapon : MonoBehaviour {
    6.  
    7.     public enum WeaponType { oneHanded, twoHanded, DualHanded }
    8.     public WeaponType weaponType = WeaponType.oneHanded;
    9.  
    10.     public enum FireMode { Automatic, Manual}
    11.     public FireMode fireMode = FireMode.Automatic;
    12.  
    13.  
    14.     public SetTorsoSprite Torso;
    15.  
    16.     public float fireSpeed;
    17.  
    18.     public Rigidbody2D bullet;
    19.     public float bulletSpeed;
    20.  
    21.     public GameObject muzzleFlash;
    22.     public Transform muzzlePos;
    23.  
    24.  
    25.     public Camera mainCam;
    26.     public float offset;
    27.  
    28.     public bool canShoot;
    29.  
    30.     void Start () {
    31.         Torso.SetSprite((int)weaponType);
    32.     }
    33.  
    34.     void Update()
    35.     {
    36.         if (fireMode == FireMode.Automatic)
    37.         {
    38.             if (Input.GetButton("Fire1"))
    39.             {
    40.                 if (canShoot)
    41.                 {
    42.                     StartCoroutine(Fire());
    43.                 }
    44.             }
    45.         }
    46.         else if (fireMode == FireMode.Manual)
    47.         {
    48.             if (Input.GetButtonDown("Fire1"))
    49.             {
    50.                 if (canShoot)
    51.                 {
    52.                     StartCoroutine(Fire());
    53.                 }
    54.             }
    55.         }
    56.     }
    57.     IEnumerator Fire()
    58.     {
    59.         Vector3 mousePos = Input.mousePosition;
    60.         mousePos.z = 5.23f;
    61.         Vector3 objectPos = Camera.main.WorldToScreenPoint(transform.position);
    62.         mousePos.x = mousePos.x - objectPos.x;
    63.         mousePos.y = mousePos.y - objectPos.y;
    64.         float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
    65.         GameObject muzzle = Instantiate(muzzleFlash, muzzlePos.position, Quaternion.Euler(new Vector3(0, 0, angle )));
    66.         Rigidbody2D bPrefab = Instantiate(bullet, muzzlePos.position, Quaternion.Euler(new Vector3(0, 0, angle + offset))) as Rigidbody2D;
    67.         bPrefab.AddForce(mousePos * bulletSpeed);
    68.         canShoot = false;
    69.         yield return new WaitForSeconds(fireSpeed);
    70.         canShoot = true;
    71.         StopCoroutine(Fire());
    72.     }
    73. }
    74.  

    Thanks in Advance !
    Greetings xTeare
     
    Last edited: Nov 20, 2016
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    *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
     
  3. DarkToadster

    DarkToadster

    Joined:
    Sep 10, 2015
    Posts:
    18
    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
     
    Last edited: Nov 20, 2016
    Lethn likes this.