So I am trying to have an animation at a player’s location anywhere on the map, but I don;t know how to go about doing that. My current code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AttackAnimation : MonoBehaviour {
public Animator anim;
void Update() {
if(Input.GetKeyDown(KeyCode.Space)) {
anim = GetComponent<Animator>();
anim.SetBool("Attack", true);
anim.SetBool("Attack", false);
}
}
}
My animator:
Thanks!
put the “anim = GetComponent();” in the start function, you just need to get that once not every time you hit space. and you are setting "attack to true and false at the same time. Your Animator needs work. The player default animation should be an “idle” or something and the transition go to “attack” with the condition = true, and back to idle when condition is false or animation has ended. As is you have a transition going to pretty much nothing and staying there. The Attack could also just come from the “any state” node. this way he attacks if he is doing anything, running walking,idling,picking his nose etc.You also want to not interrupt the attack by hitting the space bar again until attack is over. so make a bool “attacking” and set it to true while attacking and false when over so you can attack again. The condition of attacking false is added to the condition of pressing the space bar.do this with a coroutine. how ever long that attack animation is will be the value of the float that will set attacking to false again so you can attack again
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AttackAnimation : MonoBehaviour {
public Animator anim;
bool attacking;
public float attackTime;
void Start(){
anim = GetComponent<Animator>();
}
void Update() {
if(Input.GetKeyDown(KeyCode.Space)&& attacking==false) {
anim.SetBool("Attack", true);
attacking =true;
StartCoroutine(DelayAfterAttack());
}
}
private IEnumerator DelayAfterAttack(){
yield return new WaitForSeconds(attackTime);
anim.SetBool("Attack", false);
attacking=false;
}
}