Saturday, February 2, 2019

Unity 3d Shoot scripts for all guns by allunityscripts

                                                       RaycastShoot Script



                                                         





using UnityEngine;
using System.Collections;

public class RaycastShoot : MonoBehaviour {

    public int gunDamage = 1;
    public float fireRate = 0.25f;
    public float weaponRange = 50f;
    public float hitForce = 100f; 
    public Transform gunEnd;
    public GameObject Particle;

public GameObject zombie;
    public Animator zombieAnimator;

    private Camera fpsCam;                                           
    private WaitForSeconds shotDuration = new WaitForSeconds(0.07f);
    private AudioSource gunAudio;
    private LineRenderer laserLine;
    private float nextFire;


    void Start ()
    {
        laserLine = GetComponent<LineRenderer>();

        gunAudio = GetComponent<AudioSource>();

        fpsCam = GetComponentInParent<Camera>();
    }
 

    void Update ()
    {
        if (Input.GetButtonDown("Fire1") && Time.time > nextFire)
        {
            nextFire = Time.time + fireRate;

            StartCoroutine (ShotEffect());

            Vector3 rayOrigin = fpsCam.ViewportToWorldPoint (new Vector3(0.5f, 0.5f, 0.0f));

            RaycastHit hit;

            laserLine.SetPosition (0, gunEnd.position);

            if (Physics.Raycast (rayOrigin, fpsCam.transform.forward, out hit, weaponRange))
            {
                laserLine.SetPosition (1, hit.point);
            if (hit.rigidbody != null)
            {
if(hit.collider.tag == "Enemy") {
zombieAnimator = hit.transform.gameObject.GetComponent<Animator> ();
zombieAnimator.SetTrigger ("Death");
zombie = hit.transform.gameObject;
                zombie.GetComponent<Enemy_AI>().isDead = true;
                }
                hit.rigidbody.AddForce (-hit.normal * hitForce);
            }
          }
            else
            {
                laserLine.SetPosition (1, rayOrigin + (fpsCam.transform.forward * weaponRange));
            }
        }
    }


    private IEnumerator ShotEffect()
    {
        gunAudio.Play ();

        //laserLine.enabled = true;

        yield return shotDuration;

        laserLine.enabled = false;
    }
}

No comments:

Post a Comment