- Home /
Question by
ElementalVenom · Sep 28, 2015 at 06:40 AM ·
unity 5animator
Animation in animator not always working(only sometimes)
Hey guys me again. So ive just recently started using animators instead of just the animation because im making more and more games that are multiplayer. So the problem im having is this: I have a script that is suppose to play a shooting animation when i click and deduct 1 ammo point. Its deducting the ammo but only plays the animation every 4-6 shots. Can you help me? Code: using UnityEngine; using System.Collections;
public class Shooting : MonoBehaviour {
int ammo;
int maxAmmo;
int weapon=1;
int pocketAmmo;
int maxPocketAmmo=32;
GameObject eyes;
bool canShoot=true;
// Use this for initialization
void Start () {
if(weapon==1){
pocketAmmo=maxPocketAmmo;
maxAmmo=8;
ammo=maxAmmo;
}
GameObject Head=GetComponent<PlayerController>().Head;
eyes=Head.transform.FindChild("Eyes").gameObject;
}
public void buyAmmo(){
if(GetComponent<Shop>().getMoney()>=50){
addAmmo(32);
ammo=maxAmmo;
GetComponent<Shop>().removeMoney(50);
}
}
public void addAmmo(int ammount){
pocketAmmo=maxPocketAmmo;
}
// Update is called once per frame
void Update () {
if(Input.GetMouseButtonDown(0) && ammo>0 && canShoot){
ammo-=1;
//if(GetComponent<Animator>().GetCurrentAnimatorStateInfo(1).IsName("Glock-Shoot")){
Debug.Log("Fired");
transform.GetComponent<Animator>().SetBool("isShootingGlock",true);
//}
shoot(1);
StartCoroutine("shootTimer",0.317f);
}
if(Input.GetKeyDown(KeyCode.R) && ammo<8 && pocketAmmo>0){
int tmpInt=0;
if(pocketAmmo>=maxAmmo){
tmpInt=maxAmmo-ammo;
pocketAmmo-=tmpInt;
ammo=maxAmmo;
}else{
tmpInt=0;
tmpInt=maxAmmo-ammo;
if(pocketAmmo>=tmpInt){
pocketAmmo-=tmpInt;
}else{
ammo+=pocketAmmo;
pocketAmmo=0;
}
}
}
}
IEnumerator shootTimer(float waitTime){
yield return new WaitForSeconds(waitTime);
transform.GetComponent<Animator>().SetBool("isShootingGlock",false);
canShoot=true;
}
void shoot(int Damage){
canShoot=false;
RaycastHit hit;
Physics.Raycast(transform.position,eyes.transform.forward, out hit,99);
if(hit.transform.tag=="Zombie" && hit.transform.GetComponent<Health>()!=null && hit.transform.GetComponent<Health>().dieID==2){
hit.transform.GetComponent<PhotonView>().RPC("takeDamage",PhotonTargets.AllBuffered,2);
}
}
void OnGUI(){
GUI.Box(new Rect(Screen.width-Screen.width/6,Screen.height-Screen.height/6,Screen.width/7,Screen.height/7),"");
GUI.skin.label.fontSize=Screen.height/12;
GUI.Label(new Rect(Screen.width-Screen.width/7,Screen.height-Screen.height/9.3f,Screen.width/5,Screen.height/5),ammo.ToString());
if(pocketAmmo>9){
GUI.skin.label.fontSize=Screen.height/7;
GUI.Label(new Rect(Screen.width-Screen.width/8,Screen.height-Screen.height/6,Screen.width/5,Screen.height/5),pocketAmmo.ToString());
}
else{
GUI.skin.label.fontSize=Screen.height/7;
GUI.Label(new Rect(Screen.width-Screen.width/9,Screen.height-Screen.height/6,Screen.width/5,Screen.height/5),"0"+pocketAmmo.ToString());
}
}
}
Comment
Best Answer
Answer by ElementalVenom · Oct 07, 2015 at 09:42 PM
Fixed by setting the animation to not have a transition time. As in it instantly changes animation state.
@ElementalVenom where is that transition time settings located?