- Home /
Question by
jetstyler1199 · Apr 28, 2020 at 04:35 AM ·
animationanimatorgetkeydown
animation only plays first few frames when keydown
im trying to make it so that when i press the mouse0 button it would play the shooting animation,but for some reason it only plays the first frame then returns back to the idle like its interrupting it, how do you fix this?
heres the code
public class PlayersController2D : MonoBehaviour
{
public GameObject shots;
public Transform ShotSpawn;
private object shotRef;
Animator animator;
public bool isShooting;
public float shootDelay = .5f;
Rigidbody2D rb2d;
SpriteRenderer spriteRenderer;
bool isGrounded;
public Transform groundCheckR;
public Transform groundCheckL;
public Transform groundCheckC;
public Transform groundCheckCR;
public Transform groundCheckCL;
[SerializeField]
private float runspeed =1.5f;
[SerializeField]
private float jumpspeed = 5f;
bool isFacingLeft;
// Start is called before the first frame update
void Start()
{
shotRef = Resources.Load("Shots");
animator = GetComponent<Animator>();
rb2d = GetComponent<Rigidbody2D>();
spriteRenderer = GetComponent<SpriteRenderer>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Mouse0))
{
if (isShooting) return;
//shoot
animator.Play("Shoot");
isShooting = true;
Invoke("ResetShoot", shootDelay);
//instantiate and shoot Shot
GameObject s = Instantiate(shots);
s.GetComponent<shotscript>().StartShoot(isFacingLeft);
s.transform.position = ShotSpawn.transform.position;
}
}
void ResetShoot()
{
isShooting = false;
animator.Play("Idle Animation");
}
private void FixedUpdate()
{
if((Physics2D.Linecast(transform.position, groundCheckR.position, 1 << LayerMask.NameToLayer("Ground"))) ||
(Physics2D.Linecast(transform.position, groundCheckL.position, 1 << LayerMask.NameToLayer("Ground"))) ||
(Physics2D.Linecast(transform.position, groundCheckC.position, 1 << LayerMask.NameToLayer("Ground"))) ||
(Physics2D.Linecast(transform.position, groundCheckCL.position, 1 << LayerMask.NameToLayer("Ground"))) ||
(Physics2D.Linecast(transform.position, groundCheckCR.position, 1 << LayerMask.NameToLayer("Ground"))))
{
Debug.Log("on Ground");
isGrounded = true;
}
else
{
Debug.Log("not on Ground");
isGrounded = false;
animator.Play("Fall Animation");
}
if (Input.GetKey("d") || Input.GetKey("right"))
{
rb2d.velocity = new Vector2(runspeed, rb2d.velocity.y);
if(isGrounded)
animator.Play("Run Animation");
spriteRenderer.flipX = false;
isFacingLeft = false;
}
else if (Input.GetKey("a") || Input.GetKey("left"))
{
rb2d.velocity = new Vector2(-runspeed, rb2d.velocity.y);
if (isGrounded)
animator.Play("Run Animation");
spriteRenderer.flipX = true;
isFacingLeft = true;
}
else
{
if (isGrounded)
animator.Play("Idle Animation");
rb2d.velocity = new Vector2(0, rb2d.velocity.y);
}
if (isGrounded == true && Input.GetKey("space"))
{
rb2d.velocity = new Vector2(rb2d.velocity.x,jumpspeed);
animator.Play("Jump Animation");
}
}
}
Comment
Your answer
Follow this Question
Related Questions
2D Animation does not start 1 Answer
Animator - Animation State normalizedTime is not normalized 2 Answers
Issue with mecanim playing an animation using setbool 1 Answer
Imported Blender Animations not Working 2 Answers
Error console animator 0 Answers