Activate animation at player's location
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!
Answer by shadowpuppet · May 05, 2020 at 12:49 PM
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;
}
}
so to clarify, when the script starts, it gets the animator component and the bool "attacking" is false. when you hit the space bar, a transition from the default state in the animator leading to the attack animation with the condition of "Attack" being true. This also sets a bool in the script saying that an attack is taking place (attacking = true) so hitting the space bar again while the attack animation is playing will do nothing. After you hit the space bar, in addition to triggering the "Attack animation" and setting the attacking bool to true, you started a coroutine - another instruction for the script to carry out that will occur after a delay. The delay is the float "attackTime" and that value should be the length of time of the attack animation or a little less. This coroutine will reset the attacking bool in the script back to false (attacking=false) and allow you to attack again with the space bar. But in order for any of this to work you need to fix the Animator
How can I set the condition for true in the animator though?
Great, thanks, it works! But how can I spawn it at the player's or enemy's location if I may ask?
not sure what you mean. If the player has the Animator component, and the Animator Controller is dragged into the controller slot on that component, it travels with the player. It is attached to the player like any other script. So no matter where the player is all his scripts go with him. His Animator,his Attack script, his Colliders, rigidbody etc, everything in the inspector window that shows up when you click on the player - all that goes where he goes. Your other question you probably know since you said the script works, but you set the Attack bool in the Animator with that lie of code - anim.SetBool("Attack", true); You also set the attacking bool to true as well, but that has nothing to do with the Animator - it is just a condition that must me met in the Attack script for the attack to take place - the bool Attack to be set true in the Animator http://www.dlavender.com/diagram.png
Your answer
Follow this Question
Related Questions
2d animation mixing 1 Answer
2D animation activates all child objects when it starts. 0 Answers
Help with 8-DOM Weapon Sprite Issue 0 Answers
2D Spine Animation not playing 0 Answers