using UnityEngine;
using System.Collections;
public class RaycastShootComplete : 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;
public EnemyGenerator enemyGenerator;
private Camera fpsCam;
private WaitForSeconds shotDuration = new WaitForSeconds(0.07f);
private AudioSource gunAudio;
private LineRenderer laserLine;
private float nextFire;
private Animator anim;
void Start ()
{
anim = GetComponent<Animator> ();
// Get and store a reference to our LineRenderer component
laserLine = GetComponent<LineRenderer>();
// Get and store a reference to our AudioSource component
gunAudio = GetComponent<AudioSource>();
// Get and store a reference to our Camera by searching this GameObject and its parents
fpsCam = GetComponentInParent<Camera>();
}
private IEnumerator ShotEffect()
{
// Gun.getInstance.PlayAnim ();
// this.gameObject.GetComponentInParent<Rigidbody>() .AddForce (new Vector3 (-50f, 0, 0), ForceMode.Impulse);
GameObject particle = Instantiate (Particle, gunEnd.transform.position, Particle.transform.rotation);
Destroy (particle, 0.5f);
// Play the shooting sound effect
gunAudio.Play ();
// Turn on our line renderer
//laserLine.enabled = true;
//Wait for .07 seconds
yield return shotDuration;
// Deactivate our line renderer after waiting
laserLine.enabled = false;
}
public void Fire()
{
if ( Time.time > nextFire )
{//Input.GetButtonDown("Fire1")&& Time.time > nextFire
// Update the time when our player can fire next
nextFire = Time.time + fireRate;
anim.SetTrigger("Shooting");
// Start our ShotEffect coroutine to turn our laser line on and off
StartCoroutine(ShotEffect());
// Create a vector at the center of our camera's viewport
Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.0f));
// Declare a raycast hit to store information about what our raycast has hit
RaycastHit hit;
// Set the start position for our visual effect for our laser to the position of gunEnd
laserLine.SetPosition(0, gunEnd.position);
// Check if our raycast has hit anything
if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, weaponRange))
{
// Set the end position for our laser line
laserLine.SetPosition(1, hit.point);
// Check if the object we hit has a rigidbody attached
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;
enemyGenerator.SpawnZombie();
}
// Add force to the rigidbody we hit, in the direction from which it was hit
hit.rigidbody.AddForce(-hit.normal * hitForce);
}
}
else
{
// If we did not hit anything, set the end of the line to a position directly in front of the camera at the distance of weaponRange
laserLine.SetPosition(1, rayOrigin + (fpsCam.transform.forward * weaponRange));
}
}
}
}
using System.Collections;
public class RaycastShootComplete : 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;
public EnemyGenerator enemyGenerator;
private Camera fpsCam;
private WaitForSeconds shotDuration = new WaitForSeconds(0.07f);
private AudioSource gunAudio;
private LineRenderer laserLine;
private float nextFire;
private Animator anim;
void Start ()
{
anim = GetComponent<Animator> ();
// Get and store a reference to our LineRenderer component
laserLine = GetComponent<LineRenderer>();
// Get and store a reference to our AudioSource component
gunAudio = GetComponent<AudioSource>();
// Get and store a reference to our Camera by searching this GameObject and its parents
fpsCam = GetComponentInParent<Camera>();
}
private IEnumerator ShotEffect()
{
// Gun.getInstance.PlayAnim ();
// this.gameObject.GetComponentInParent<Rigidbody>() .AddForce (new Vector3 (-50f, 0, 0), ForceMode.Impulse);
GameObject particle = Instantiate (Particle, gunEnd.transform.position, Particle.transform.rotation);
Destroy (particle, 0.5f);
// Play the shooting sound effect
gunAudio.Play ();
// Turn on our line renderer
//laserLine.enabled = true;
//Wait for .07 seconds
yield return shotDuration;
// Deactivate our line renderer after waiting
laserLine.enabled = false;
}
public void Fire()
{
if ( Time.time > nextFire )
{//Input.GetButtonDown("Fire1")&& Time.time > nextFire
// Update the time when our player can fire next
nextFire = Time.time + fireRate;
anim.SetTrigger("Shooting");
// Start our ShotEffect coroutine to turn our laser line on and off
StartCoroutine(ShotEffect());
// Create a vector at the center of our camera's viewport
Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.0f));
// Declare a raycast hit to store information about what our raycast has hit
RaycastHit hit;
// Set the start position for our visual effect for our laser to the position of gunEnd
laserLine.SetPosition(0, gunEnd.position);
// Check if our raycast has hit anything
if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, weaponRange))
{
// Set the end position for our laser line
laserLine.SetPosition(1, hit.point);
// Check if the object we hit has a rigidbody attached
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;
enemyGenerator.SpawnZombie();
}
// Add force to the rigidbody we hit, in the direction from which it was hit
hit.rigidbody.AddForce(-hit.normal * hitForce);
}
}
else
{
// If we did not hit anything, set the end of the line to a position directly in front of the camera at the distance of weaponRange
laserLine.SetPosition(1, rayOrigin + (fpsCam.transform.forward * weaponRange));
}
}
}
}
No comments:
Post a Comment