Question by
paraicseosamhcanavan05 · Jul 03, 2020 at 02:04 AM ·
c#movement2d-platformerattackattacking
Charecter keeps playing walking animation after punching
using System.Collections; using System.Collections.Generic; using System.Security.Cryptography; using UnityEngine;
public class playercontroller : MonoBehaviour { Animator animator; Rigidbody2D rb2d; SpriteRenderer spriteRenderer;
bool isGrounded;
bool isAttacking = false;
[SerializeField]
Transform groundCheck;
[SerializeField]
private float runSpeed = 1.5f;
[SerializeField]
private float jumpSpeed = 5.1f;
// Start is called before the first frame update
void Start()
{
animator = GetComponent<Animator>();
rb2d = GetComponent<Rigidbody2D>();
spriteRenderer = GetComponent<SpriteRenderer>();
}
void Update()
{
if (Input.GetKey("z"))
{
isAttacking = true;
animator.Play("Punch");
Invoke("ResetAttack", .5f);
}
void ResetAttack()
{
isAttacking = false;
}
if (Input.GetKey("d") || Input.GetKey("right"))
{
rb2d.velocity = new Vector2(2, rb2d.velocity.y);
if (isGrounded)
{
animator.Play("Walk");
}
else if (!isGrounded)
{
animator.Play("Jump");
}
//spriteRenderer.flipX = false;
transform.localScale = new Vector3(1, 1, 1);
}
else if (Input.GetKey("a") || Input.GetKey("left"))
{
rb2d.velocity = new Vector2(-2, rb2d.velocity.y);
if (isGrounded)
{
animator.Play("Walk");
}
else if (!isGrounded)
{
animator.Play("Jump");
}
//spriteRenderer.flipX = true;
transform.localScale = new Vector3(-1, 1, 1);
}
else
{
if (isGrounded && !isAttacking)
animator.Play("Idle");
rb2d.velocity = new Vector2(0, rb2d.velocity.y);
}
if (Input.GetKey("x") && isGrounded && !isAttacking|| Input.GetKey("space") && isGrounded && !isAttacking)
{
rb2d.velocity = new Vector2(rb2d.velocity.x, jumpSpeed);
animator.Play("Jump");
}
}
// Update is called once per frame
private void FixedUpdate()
{
if(Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground")))
{
isGrounded = true;
}
else
{
isGrounded = false;
}
}
}
i was following a tutorial and messed up somewhere help https://www.youtube.com/watch?v=KamdeKs6eKo,
Comment
Your answer
Follow this Question
Related Questions
Take damage only when attack animation is done? 1 Answer
Help making my 2D platformer character jump? 0 Answers
Help 2D attack 1 Answer
2D plaformer, Can bug me into wall 2 Answers