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

Need help with my code

Discussion in 'Scripting' started by EXTREME_S, Jul 25, 2020.

  1. EXTREME_S

    EXTREME_S

    Joined:
    Jul 24, 2020
    Posts:
    10
    I coded a bullet shooting system and when I click it spawns a lot of bullets, Please help me



    Heres My Gun code


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

    public class Gun : MonoBehaviour
    {
    public float damage = 10f;
    public float range = 100f;
    public float fireRate = 15f;
    public float impactForce = 30f;

    public Camera MainCamera;
    public ParticleSystem GunShot;
    public GameObject impactEffect;
    public GameObject bulletPrefab;

    public Transform bulletSpawn;

    public float bulletSpeed = 30;

    public float lifeTime = 3;

    private float nextTimeToFire = 0f;

    // Update is called once per frame
    void Update()
    {
    if (Input.GetButton("Fire1"))
    {
    Fire();
    }



    Heres my bullet code


    using UnityEngine;

    public class Bullet : MonoBehaviour
    {
    private void OnTriggerEnter(Collider other)
    {
    print("hit " + other.name + "!");
    Destroy(gameObject);
    }

    }



    if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
    {
    nextTimeToFire = Time.time + 1f / fireRate;
    Shoot();
    }
    }


    private void Fire()
    {
    GameObject bullet = Instantiate(bulletPrefab);

    Physics.IgnoreCollision(bullet.GetComponent<Collider>(),
    bulletSpawn.parent.GetComponent<Collider>());

    bullet.transform.position = bulletSpawn.position;

    Vector3 rotation = bullet.transform.rotation.eulerAngles;

    bullet.transform.rotation = Quaternion.Euler(rotation.x, transform.eulerAngles.y, rotation.z);

    bullet.GetComponent<Rigidbody>().AddForce(bulletSpawn.forward * bulletSpeed, ForceMode.Impulse);

    StartCoroutine(DestroyBulletAfterTime(bullet, lifeTime));
    }

    private IEnumerator DestroyBulletAfterTime(GameObject bullet, float delay)
    {
    yield return new WaitForSeconds(delay);

    Destroy(bullet);
    }

    void Shoot()
    {
    GunShot.Play();

    RaycastHit hit;
    if (Physics.Raycast(MainCamera.transform.position, MainCamera.transform.forward, out hit, range))
    {
    Debug.Log(hit.transform.name);

    Target target = hit.transform.GetComponent<Target>();

    if (target != null) ;
    {
    target.TakeDamage(damage);
    }

    if (hit.rigidbody != null)
    {
    hit.rigidbody.AddForce(-hit.normal * impactForce);
    }

    GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
    Destroy(impactGO, 2f);
    }
    }
    }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    What's your desired behavior?
     
  3. EXTREME_S

    EXTREME_S

    Joined:
    Jul 24, 2020
    Posts:
    10
    Sorry I'm new to C# what does that mean?
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    It has nothing to do with C#. You said
    So presumably "spawning a lot of bullets" is not what you want to happen. But it's hard to help you if you haven't explained what you actually want to happen. Do you want just a single bullet to fire per mouse click? Do you want a lot of them, but fewer than currently? Do you want something completely different to happen? Help me help you.
     
  5. EXTREME_S

    EXTREME_S

    Joined:
    Jul 24, 2020
    Posts:
    10
    Ohhh, Ok so i realized what i did wrong but i still have one more thing, I want the bullet to go in a straight line instead of going straight down. like a normal pistol would do
     
  6. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
    You probably have gravity enabled on the rigidbody. Turn off gravity if you want it to go in a straight line.

    Also, just watch out trying to use rigidbodies for bullets. Unless your game is cartoony, and the bullets are slow, you'll probably have a lot of cases where the bullets are going too fast and don't trigger colliders as you've expect. There's a whole bunch of threads on the forums of how to handle bullets.