Question by
Betaaubrey1 · Aug 21, 2016 at 07:21 PM ·
gunammoreloading
how do add ammo and reload to my script? redo
using UnityEngine; using System.Collections;
[RequireComponent (typeof (AudioSource))] public class Gun : MonoBehaviour {
public enum GunType {Semi,Burst,Auto};
public LayerMask collisonMask;
public float gunID;
public GunType gunType;
public float rpm;
public float damage = 1;
// Components
public Transform spawn;
public Transform shellEjectionPoint;
public Rigidbody shell;
private LineRenderer tracer;
// System
private float secondsBetweenShots;
private float nextPossibleShootTime;
void Start() {
secondsBetweenShots = 60 / rpm;
if (GetComponent<LineRenderer> ()) {
tracer = GetComponent<LineRenderer> ();
}
}
public void Shoot () {
if (CanShoot ()) {
Ray ray = new Ray (spawn.position, spawn.forward);
RaycastHit hit;
float shotDistance = 20;
if (Physics.Raycast (ray, out hit, shotDistance, collisonMask)) {
shotDistance = hit.distance;
if (hit.collider.GetComponent<Entity> ()) {
hit.collider.GetComponent<Entity> ().TakeDamage(damage);
}
}
nextPossibleShootTime = Time.time + secondsBetweenShots;
GetComponent<AudioSource>().Play();
if (tracer) {
StartCoroutine ("RenderTracer", ray.direction * shotDistance);
}
Rigidbody newShell = Instantiate (shell, shellEjectionPoint.position, Quaternion.identity) as Rigidbody;
newShell.AddForce (shellEjectionPoint.forward * Random.Range (150f, 200f) + spawn.forward * Random.Range (-10f, 10f));
}
}
public void ShootContinuous() {
if (gunType == GunType.Auto) {
Shoot ();
}
}
private bool CanShoot() {
bool canShoot = true;
if (Time.time < nextPossibleShootTime) {
canShoot = false;
}
return canShoot;
}
IEnumerator RenderTracer(Vector3 hitPoint) {
tracer.enabled = true;
tracer.SetPosition (0, spawn.position);
tracer.SetPosition (1, spawn.position + hitPoint);
yield return null;
tracer.enabled = false;
}
}
Comment
Your answer
Follow this Question
Related Questions
My gun reloading dont work,can i please get help. 2 Answers
how do i add ammo and reloading to this script 1 Answer
Gun not reloading properly & time not deducting correctly when below 10 seconds 2 Answers
how to display the correct numbers of ammunition per weapons 0 Answers
Why did my boolean don't work? 0 Answers