- Home /
Question by
Bruno-Fludzinski · Feb 06, 2013 at 05:12 PM ·
gunreloadreloadingshot
Reload after certain amount of shots?
Here is my (machine) gun script:
var fireRate : float = 0.1;
var Prefab: Transform;
private var nextFire = 0.0;
var speed : float = 50;
function Update () {
if(Input.GetKey("mouse 0")&&Time.time > nextFire){
nextFire = Time.time + fireRate;
var copy = Instantiate(Prefab,GameObject.Find("spawnPoint").transform.position,transform.rotation);
copy.rigidbody.velocity = transform.TransformDirection(Vector3.forward * speed);
}
}
and what I want to do is say when I reach 20 shots I have to press r to reload also a box in the corner saying how many shots I have left or how many shots I've taken. Thank you
Comment
Simply add in a counter for your ammo.
public var bullets : int = 20;
public var maxBullets : int = 20;
public var reloadTime : float = 1;
private var reloading : boolean = false;
function Update(){
if(bullets > 0 && !reloading &&**other conditions**){
//other stuff
bullets -- // use up one bullet
}
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.R)){
Reload();
}
}
function Reload(){
reloading = true;
yield WaitForSecond(reloadTime);
reloading = false;
bullets = maxBullets;
}
Sidenote: Ins$$anonymous$$d of transform.TransformDirection(Vector3.forward) you can use transform.forward.
Answer by Loius · Feb 06, 2013 at 05:48 PM
http://docs.unity3d.com/Documentation/ScriptReference/index.html
Look up GUI.Label and Input.GetButtonDown.
Your answer
Follow this Question
Related Questions
Reloading A Gun My Way 2 Answers
Gun - Ammo , reloading and UI problem. 1 Answer
Pass a JS Script to C#? 1 Answer
realistic reload system? 1 Answer