Question by
Betaaubrey1 · Aug 22, 2016 at 01:37 PM ·
ammo
how do i add ammo and reload to my script: this is what ive tried http://pastebin.com/wPybHpys
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
![](https://koobas.hobune.stream/wayback/20220612091330im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
AmmoScript.cs troubles... 1 Answer
Storing diffrent bullet amounts for different Weapons. 1 Answer
How to make ammo reffil on pickup 0 Answers
How to turn score points into ammo amount? 0 Answers
How can I add ammo to my gun script? 0 Answers