Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Nulim · Sep 12, 2021 at 02:29 AM · 2d-platformerprogramming2d-physicsdashmechanics

Able to dash in the air but NOT on the floor/standing tile

Hi guys,

I am implementing game mechanics to my character and for some reason I am not able to dash while moving right/left but while I am in the air/jumping, I am able to dash left or right. Here is my code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class AlienController : MonoBehaviour
 {
     float horizontalMovement;
 
     [SerializeField]
     float jumpForce = 500f, moveSpeed = 5f;
 
     Rigidbody2D rb;
     bool doubleJumpAllowed = true;
     bool onTheGround = true;
     bool isDashing = false;
     float dashCooldown;
 
     void Start () {
         rb = GetComponent<Rigidbody2D> ();
     }
     
     void Update () {
         if(onTheGround)
             isDashing = false;
 
         if(Input.GetKeyDown(KeyCode.X) && !isDashing && !onTheGround)
         {
             StartCoroutine(Dash());
         }
             
         //Jump Logic
         if (onTheGround && Input.GetButtonDown ("Jump")) 
         {
             Jump();
             onTheGround = false;
         } 
         else if (doubleJumpAllowed && Input.GetButtonDown ("Jump")) 
         {
             Jump();
             doubleJumpAllowed = false;
         }
 
         horizontalMovement = Input.GetAxis ("Horizontal") * moveSpeed;
     }
 
     void FixedUpdate()
     {
         if(!isDashing)
             rb.velocity = new Vector2 (horizontalMovement, rb.velocity.y);
     }
 
     void OnCollisionEnter2D()
     {
         onTheGround = true;
         doubleJumpAllowed = true;
         isDashing = false;
     }
     
     IEnumerator Dash()
     {
         isDashing = true;
         rb.velocity = new Vector2(Input.GetAxis ("Horizontal") * 15f, 0f);
         var gravity = rb.gravityScale;
         rb.gravityScale = 0;
         yield return new WaitForSeconds(0.5f);
         rb.gravityScale = gravity;
     }
 
     void Jump()
     {
         rb.velocity = new Vector2 (rb.velocity.x, 0f);
         rb.AddForce(Vector2.up * jumpForce);
     }
 }

If anyone is able to help me dash left/right while not jumping, I would be very grateful!

Thanks and have a wonderful day!

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
1
Best Answer

Answer by zhavens88 · Sep 12, 2021 at 08:20 AM

Hello, i think you want !isGrounded to be isGrounded on your X button keycheck. you have..

 if(Input.GetKeyDown(KeyCode.X) && !isDashing && !onTheGround)

Basically you've said "if im not dashing and NOT on the ground, Then dash.

Should be

 if(Input.GetKeyDown(KeyCode.X) && !isDashing && isGrounded)

"if im not dashing and im ON the ground"

im no pro so hopefully that helps. i cant tell if anything else is wrong ( it all looks pretty good though)

Comment
Add comment · Show 1 · 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
avatar image Nulim · Sep 12, 2021 at 01:46 PM 0
Share

Thanks mate, that fixed it for me! Can't believe I didn't see that since it is pretty obvious when you laid out.

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

180 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Multiple Cars not working 1 Answer

Should I script my own Physics2D Platformer Character Controller just like in the Unity's Live Session? 1 Answer

Transform.position assign attempt not valid, and position is infinity on an object? 1 Answer

AddForce problem axis X 1 Answer

Problem with 2D movement. My character is moving by himself between two points... :) 1 Answer


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