• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ModularShrimp · Jan 08, 2020 at 09:34 AM · 2d gameunity 2d2d animation2d sprites2d 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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

153 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Sprite colors not displaying properly after I click play 2 Answers

Suggestions for player evolution 1 Answer

2D Sprite Fillup without using UI 1 Answer

Issue I'm having with Rule Tiles, anyone able to solve this? 1 Answer

Unity 2D Trying to get perfect relation between camera size and resolution 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges