Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Dollar-Fish · Jul 28, 2015 at 11:19 AM · playerunity 2dplatformergrounded

Player Can Only Attack When Not Grounded

I want to make it so the player can only attack while in mid-air and is not grounded. So when you first tap, he only jumps, but when you are in mid-air, he will attack. I tried and tried to figure out but could never solve it.

Player Script

 using UnityEngine;
 using System.Collections;
 
 public class Player : MonoBehaviour {
 
     public float jumpPower = 555f;
 
     public bool grounded = false;
 
     private Rigidbody2D rb2d;
 
     private playerAttack player;
 
     void Start () 
     {
         rb2d = gameObject.GetComponent<Rigidbody2D>();
         anim = gameObject.GetComponent<Animator>();
         player = GameObject.Find("Player").GetComponent<playerAttack>();
 
     }
 
     void Update() 
     {
     
         anim.SetBool("Grounded",grounded);
         anim.SetFloat("Speed", Mathf.Abs(rb2d.velocity.x));
 
         if (Input.GetMouseButtonDown(0) && grounded)
         {
             rb2d.AddForce(Vector2.up * jumpPower);
         }
         
     }
 }

playerAttack Script

 using UnityEngine;
 using System.Collections;
 
 public class playerAttack : MonoBehaviour {
     
     private bool attacking = false;
 
     private float attackTimer = 0;
     private float attackCd = 0.125f;
     public bool grounded = true;
     
     public Collider2D attackTrigger;
     private Animator anim;
     private Player player;
     
     void Start()
     {
         player = GameObject.Find("Player").GetComponent<Player>();
     }
     void Awake()
     {
         anim = gameObject.GetComponent<Animator>();
         attackTrigger.enabled = false;
     }
     void Update()
     {
 
         if (Input.GetMouseButtonDown(0) && !attacking &&  // possibly another bool)
         {
             
             attacking = true;
             attackTimer = attackCd;
 
             attackTrigger.enabled = true;
             
         }
         
         if (attacking)
         {
             if(attackTimer > 0)
             {
                 attackTimer -= Time.deltaTime;
             }
             else
             {
                 attacking = false;
                 attackTrigger.enabled = false;
             }
             
         }
 
         anim.SetBool("Attacking", attacking);
         
     }
     
 }

Ground Check Script

 using UnityEngine;
 using System.Collections;
 
 public class GroundCheck : MonoBehaviour {
 
     private Player player;
 
     void Start()
     {
         player = gameObject.GetComponentInParent<Player>();
 
     }
     
     void OnTriggerEnter2D(Collider2D col)
     {
         player.grounded = true;
     }
 
     void OnTriggerStay2D(Collider2D col)
     {
         player.grounded = true;
     }
 
     void OnTriggerExit2D(Collider2D col)
     {
         player.grounded = 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Wiki

Answer by SirMalarkey · Jul 28, 2015 at 11:48 AM

I don't really see any logic error in this code that would stop the player from attacking. My guess is that the problem is in the animator itself. When the player is touching the ground the isGrounded bool is set, so if the player attacks both the isGrounded and isAttacking bools will be set. This most likely creates a conflict where the grounded animation plays over the attack animation. when jumping the bool isGrounded is no longer set and the attacking animation plays.

~Malarkey

Comment
Add comment · Share
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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How do I get my character to dash up? 0 Answers

2D movement that allows 2 or more forces.,2D movement that allows to have 2 different forces. 0 Answers

Audio for movement 1 Answer

How To Change Player As Soon as He Take Powerup 1 Answer

2D 360 degress platformer example needed 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