- Home /
How To Disable/Enable Function
I want, if reload then not shot, How to do this? This My Shoot And Reload Script.
var Effect : Transform;
var MuzzleFlash : GameObject;
var damage = 100;
function Start(){
animation.wrapMode = WrapMode.Loop;
animation["Reload"].wrapMode = WrapMode.ClampForever;
animation["Default"].wrapMode = WrapMode.Once;
animation["Reload"].layer = 1;
animation.CrossFade("Default");
}
function Update () {
Shoot();
Reload();
}
function Shoot(){
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
if (Input.GetMouseButtonDown(0))
{
Physics.Raycast (ray, hit, 100);
MuzzleFlash.SetActive(true);
var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(particleClone.gameObject, 0.7);
Debug.Log("Mouse Down"+Time.time);
}else if(Input.GetMouseButtonUp(0)){
MuzzleFlash.SetActive(false);
Debug.Log("Mouse Up"+Time.time);
}
}
function Scope(){
if (Input.GetMouseButtonDown(1)){
}else if(Input.GetMouseButtonUp(1)){
}
}
function Reload(){
if(Input.GetKeyDown("r")){
animation.CrossFade("Reload");
}else if(Input.GetKeyUp("r")){
yield WaitForSeconds(2);
animation.Stop();
animation.CrossFade("Default");
}
}
thanks before...
Answer by xikky · Jul 21, 2013 at 07:19 AM
You can declare a boolean variable and set it to false initially. Then you switch it to true when reload is started and back to false when reload is finished. Then wrap the Shoot() function inside this boolean to execute Shoot() only when the declared boolean is false.
Answer by SuhaimKhalid · Jan 27, 2021 at 08:57 PM
You can use boolean for example shootclick and put all code of shoot function in if shootclick is true like this
function Shoot(){ if(shootclick==true) ``{ var hit : RaycastHit; var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
if (Input.GetMouseButtonDown(0))
{
Physics.Raycast (ray, hit, 100);
MuzzleFlash.SetActive(true);
var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(particleClone.gameObject, 0.7);
Debug.Log("Mouse Down"+Time.time);
}else if(Input.GetMouseButtonUp(0)){
MuzzleFlash.SetActive(false);
Debug.Log("Mouse Up"+Time.time);
}
} }
Your answer
Follow this Question
Related Questions
Wait for reload to finish? 1 Answer
reloading script problem 0 Answers
Function reload doesnt work 1 Answer
Problem with Ammo/Shoot script 2 Answers
call a function at a specific frame of animation using mecanin 2 Answers