- Home /
Question by
ModularShrimp · Jan 08, 2020 at 09:34 AM ·
2d gameunity 2d2d sprites2d animation2d platformer
2D Animation plays for a split second unless i spam the input button,2D Animation plays for a split second unless i spam the input button
when i try to perform Melee attack it only plays animation for a slip second, if i keep spamming the Fire2(Melee attack input) button it sometimes work and plays the complete random attack animation but other times the player will twitch for a split second and goes back to idle
//CHARACTER CONTROLLER
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
//PLAYER AND ANIMATION
public Rigidbody2D rb;
public Animator animate;
public SpriteRenderer sp;
public float speed = 0f;
public float jumpforce = 0f;
//JOYSTICK
public Joystick joystick;
//GroundCheck
bool isgrounded;
public Transform groundcheck;
//slide
bool running = true;
bool canslide = false;
public float waitfor = .6f;
//TO ROTATE INSTED OF FLIPPING
bool facingright = true;
bool flipp;
// TO STORE SHOOTING BOOL VALUE FROM ANOTHER SCRIPT
public bool shooting;
public bool attacking;
void Update()
{
// CHECKING IS PLAYER IS SHOOTING FROM PLAYCOMBAT SCRIPT
//SAVING VALUE INSIDE SHOOTING
PlayerCombat pc = gameObject.GetComponent<PlayerCombat>();
shooting = pc.Shooting;
//SAVING VALUE INSIDE ATTACKING
attacking = pc.attacking;
}
// Update is called once per frame
void FixedUpdate()
{
if (Physics2D.Linecast(transform.position, groundcheck.position, 1 << LayerMask.NameToLayer("ground")))
{
isgrounded = true;
}
else
{
isgrounded = false;
}
if (joystick.Horizontal >= .2f && !shooting && !attacking) //HORIZONTAL RIGHT
{
//TO TELL THE PLAYER TO FLIP LEFT
flipp = true;
rb.velocity = new Vector2(speed, rb.velocity.y);
if (isgrounded && running == true )
animate.Play("player_run");
canslide = true;
//REVERT IF ROTATE DIDN'T WORK:sp.flipX = false;
}
else if (joystick.Horizontal <= -.2f && !shooting && !attacking) //HORIZONTAL LEFT
{
flipp = false;
rb.velocity = new Vector2(-speed, rb.velocity.y);
if (isgrounded && running == true )
animate.Play("player_run");
canslide = true;
//REVERT IF ROTATE DIDN'T WORK:sp.flipX = true;
}
else
{
if (isgrounded && !shooting && !attacking) //IDLE
animate.Play("idle");
canslide = false;
waitfor = .6f;
rb.velocity = new Vector2(0, rb.velocity.y);
}
//TO TRIGGER THE RIGHT FLIP
if (flipp && !facingright)
{
flip();
}
//TO TRIGGER THE LEFT FLIP
else if (!flipp && facingright)
{
flip();
}
if (joystick.Vertical >= .6f && isgrounded == true) //JUMP
{
rb.velocity = new Vector2(rb.velocity.x, jumpforce);
animate.Play("jump");
}
if (joystick.Vertical <= -.6f && isgrounded == true && canslide == true && waitfor > 0) //SLIDE
{
waitfor -= Time.deltaTime;
running = false;
Time.timeScale = .8f;
animate.Play("player_slide");
}
else
{
running = true;
Time.timeScale = 1f;
canslide = false;
}
}
//TO ROTATE NO FLIP
private void flip()
{
facingright = !facingright;
transform.Rotate(0f, 180f, 0f);
}
}
//PLAYER COMBAT SCRIPT
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCombat : MonoBehaviour
{
Animator animator;
public bool Shooting = false;
//TO GET BULLET AND ITS FIREPOINT
public GameObject BulletPrefab;
public Transform FirePoint;
public float waitfor = 0f;
//FOR MELEE ATTACK
public bool attacking = false;
// Start is called before the first frame update
void Start()
{
animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (Input.GetButton("Jump") && waitfor >= 0f && waitfor < .7f)
{
waitfor += Time.deltaTime;
animator.Play("player_shoot");
Shooting = true; ;
}
else if (Input.GetButtonUp("Jump") && waitfor > .2f)
{
shoot();
Shooting = false;
}
else
{
waitfor = 0f;
Shooting = false;
}
if (Input.GetButtonDown("Fire2") && !Shooting && !attacking)
{
attacking = true;
//choose random attack animation
int index = UnityEngine.Random.Range(1,4);
animator.Play("player_attack" + index);
StartCoroutine(attack());
}
}
void shoot()
{
Instantiate(BulletPrefab,FirePoint.position,FirePoint.rotation);
}
IEnumerator attack()
{
yield return new WaitForSeconds(.8f);
attacking = false;
}
}
Comment