- Home /
No movement when animation plays (2d)
Hi everyone! I got this problem in the game I'm trying to make, when I press the Attack Button while holding a Movement Button (left or right), it plays the "Attack" animation but keeps moving. What I'm looking for is to stop moving if Attack animation is playing. I've tried a few things, but none of them seemed to work. Here's the code:
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour
{
public float playerSpeed;
private bool facingLeft;
private bool attacking;
private float direction;
private static exSpriteAnimation player;
void Start ()
{
player = GetComponent<exSpriteAnimation>();
playerSpeed = 100;
facingLeft = false;
attacking = false;
}
// Update is called once per frame
void Update ()
{
// Amount to move
float amtToMove = 0;
// Move the player
if(!attacking)
{
amtToMove = direction * playerSpeed * Time.deltaTime;
// Direction
direction = Input.GetAxis("Horizontal");
if( direction < 0 )
{
if(!facingLeft)
{
facingLeft = true;
player.transform.Rotate(0, 180, 0);
}
if( player.IsPlaying("Stand"))
{
player.Pause();
player.Play("Run");
}
player.transform.Translate(Vector3.left * amtToMove);
}else if( direction > 0 )
{
if(facingLeft)
{
facingLeft = false;
player.transform.Rotate(0, 180, 0);
}
if( player.IsPlaying("Stand"))
{
player.Pause();
player.Play("Run");
}
player.transform.Translate(Vector3.right * amtToMove);
}else if( direction == 0 && player.IsPlaying("Run"))
{
player.Pause();
player.Play("Stand");
}
}
// Attack
if( Input.GetKeyDown("space") )
{
attacking = true;
}else
{
attacking = false;
}
if(attacking && (player.IsPlaying("Stand") || player.IsPlaying("Run")))
{
player.Pause();
player.Play("Attack1");
}
if(player.GetCurrentAnimation() == null)
{
player.Play("Stand");
}
}
}
Any suggestions? Thanks in advance!
Comment
I used this as a guide http://www.gotoandplay.it/_articles/2006/06/beatemup_p03.php
Answer by mntcarp · Jun 27, 2012 at 01:40 AM
Nevermind, I've solved it! I put the "attcking = false" inside the last if block. Thanks anyway!