Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 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
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.

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

Suggestions for player evolution 1 Answer

Sprite colors not displaying properly after I click play 2 Answers

,Spawning snow or changing tilesets to snow as the player walks past them 0 Answers

How can I change player sprite based on mouse position relative to the player? 2 Answers

How to make weapon match walking animation (2D)? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges