- Home /
Stopping movement when animation is playing
Hey guys, so I have an issue where my attack animation will play, but the player can still move during the animation. I've tried just setting it's translation to zero, but it still moves. Here my code on how i move and play the animations:
using UnityEngine;
using System.Collections;
// Require these components when using this script
[RequireComponent(typeof(Animator))]
[RequireComponent(typeof(CapsuleCollider))]
[RequireComponent(typeof(Rigidbody))]
public class Wolf_AnimationInput : MonoBehaviour {
public float animSpeed = 1.5f; // a public setting for overall animator animation speed
public float lookSmoother = 3f; // a smoothing setting for camera motion
public float moveSpeed = 2.0f;
public float rotateSpeed = 180;
public float smooth = 0.5f;
private Animator anim;
private AnimatorStateInfo currentBaseState;// a reference to the animator on the character
private CapsuleCollider col; // a reference to the capsule collider of the character
private CharacterController controller;
private Transform _mytransform;
static int idleState = Animator.StringToHash("Base Layer.Idle");
static int runState = Animator.StringToHash("Base Layer.Run");
static int attackState = Animator.StringToHash("Base Layer.Attacks");
static float translation;
// Use this for initialization
void Start () {
_mytransform = transform;
anim = GetComponent<Animator>();
col = GetComponent<CapsuleCollider>();
}
// Update is called once per frame
void Update () {
translation = Input.GetAxis("Vertical") * moveSpeed;
currentBaseState = anim.GetCurrentAnimatorStateInfo(0);
MovePlayer();
PlayerAttack();
RotatePlayer();
/* //------Saving for camera use------//
Quaternion target = Quaternion.Euler(0, tiltAround, 0);
transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);
*/
}
void MovePlayer()
{
anim.SetFloat("Speed", translation);
anim.speed = animSpeed;
translation *= Time.deltaTime;
transform.Translate(0, 0, translation);
}
void PlayerAttack()
{
if (currentBaseState.nameHash == idleState)
{
if (Input.GetButtonDown("Attack") && translation < 0.01)
{
anim.SetBool("Atk", true);
}
else
{
anim.SetBool("Atk", false);
}
}
}
void RotatePlayer()
{
if (Mathf.Abs(Input.GetAxis("Horizontal")) > 0)
{
_mytransform.Rotate(0, Input.GetAxis("Horizontal") * Time.deltaTime * rotateSpeed, 0);
}
if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow))
{
transform.RotateAround(transform.position, transform.up, 180f);
}
else
{
}
}
}
Answer by smoggach · Aug 11, 2014 at 08:49 PM
Wrap your MovePlayer() in a check to see if your animation's "Atk" bool is set or not.
if (!anim.GetBool("Atk"))
MovePlayer();
or do the same for the relevant section of code inside MovePlayer();
It works, like the my character stops moving, but now the attack animation I have keeps playing, like the animation is always set back to true.
Your answer
Follow this Question
Related Questions
SpriteManager 2 1 Answer
Character controller + animation of different meshes making one character 1 Answer
Sprite animation 2 Answers
How to make an enemy pace? 1 Answer
Move Object Using iTween 2 Answers