Gun recoil and gun reload

Okay I have been working on my first game and I was going to publish the demo. But I cant find out how to make a gun recoil and gun reload. I have the Aiming and every thing done its just what i want to do is when you reload. It takes like 2 seconds before you get your clip full. like “yield WaitForSeconds” i cant use that because I put it in function Upate…
and I am using java…
thanks

                                             -Logan

It is difficult to help you without seeing your script One simple way around the Update() issue and waiting for a reload is to have a boolean value 'isReloading' used as a flag, and the have it set and cleared in a coroutine. In Upadate() you only allow the gun to fire if the isReloading flag is false,

do you have reload animation for your weapon and (for recoil )is your weapon is semi automatic or full auto(ie pistol or assualt rifle)

it is a full auto and I dont want to post my script because its over 900 lines

2 Answers

2

Well here’s one way you could do the reloading:

public var reloading : boolean; //Are we reloading?
public var reloadTime : float; //Time it takes to reload.

private var reloadTimer : float; //Var that keeps track of time.

function Update()
{
	//If we press reload and we aren't already reloading...
	if (!reloading && Input.GetButtonDown("ReloadButton"))
	{
		reloading = true; //Start reloading.
		reloadTimer = Time.time + reloadTime; //Set time to stop reloading.
	}
	
	//If we're reloading and the current time is more than the stop time...
	if (reloading && Time.time > reloadTimer)
	{
		reloading = false; //Stop reloading.
	}
}

Okay thanks

Hi here is the script

var reload:boolean = false;
var weapon:Transform;
var ReloadAnim:AnimationClip;
var maxrot:float = 5;///rotate your weapon and copy y axis rotation
function Update(){
if(reload){
weapon.animation.CrossFade(ReloadAnim.name);
if(!weapon.animation.IsPlaying(ReloadAnim.name)){
///amooinclp = ammoperclip;
reload = false;
}
weapon.localRotation.y = Mathf.Lerp(weapon.localRotation.y,0,Time.deltaTime * 2);
if(Input.GetButton("Fire1")){
weapon.localRotation.y = Mathf.Lerp(weapon.localRotation.y,maxrot,Time.deltaTime * 2);
}
}
}