Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
  • Help Room /
avatar image
0
Question by TimeKillerGames · Mar 05, 2021 at 11:05 AM · velocityphysics2dplatformerplayer movementdash

Unity Player bumps when falls and dash

Hello, I made a dash for my game, but when it dashes while falling it wont directly dash to the right or lleft, it will go up depending on how fast player was falling and dash.When Dashing during jump seems no problems at all. I made a video(https://www.youtube.com/watch?v=t7d2v65wYwo&ab_channel=V.D.), on 3rd attempt of fall dash on it, there you can clearly see my bug. Here is the whole code for Player Movement

 public class PlayerController : MonoBehaviour
 {
     [Header("Player Speed")]
     public float runSpeed;
     private Rigidbody2D rb;
     private float horitonalMove;
     public static bool facingRight;
     [Header("Jump")]
     public float jumpForce;
     private float jumpTime;
     public float jumpHoldTime;
     private bool isJumping;
     public int jumpCount;
     private int currentJumpCount;
     public float fallMultiplier;
     [Header("Jump Checks")]
     public Transform groundCheck;
     public float groundCheckRadius;
     public LayerMask whatIsGround;
     private bool isGrounded;
 
     [Header("Dash")]
     public float dashForce;
     public float dashTimer;
     private bool isDashing;
     private float currentDashTimer;
     private float initialGravity;
    
     void Start()
     {
         currentDashTimer = dashTimer;
         rb = gameObject.GetComponent<Rigidbody2D>();
         facingRight = true;
         initialGravity = rb.gravityScale;
     }
 
     void Update()
     {
         horitonalMove = Input.GetAxisRaw("Horizontal");
 
         if (Input.GetKeyDown(KeyCode.Space) && currentJumpCount > 0)
         {
 
             isJumping = true;
             Jump();
         }
         if (Input.GetKey(KeyCode.Space) && isJumping == true)
         {
             JumpHigher();
         }
         else
         {
             jumpTime = jumpHoldTime;
         }
         if (Input.GetKeyUp(KeyCode.Space))
         {
             isJumping = false;
         }
         Move();
         if (isDashing)
         {
             Debug.Log(rb.velocity);
             rb.velocity = new Vector2(rb.velocity.x, 0f);
             Dash();
         }
         if (Input.GetKeyDown(KeyCode.E))
         {
             rb.velocity = Vector2.zero;
             Debug.Log(rb.velocity);
             isDashing = true;
             currentDashTimer = dashTimer;
         }
         DashGravityController();
         FlipController();
         SetCameraOffset();
         JumpController();
     }
     private void FixedUpdate()
     { 
         isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
     }
 
     public void FlipController()
     {
         if (facingRight == true && horitonalMove < 0)
         {
             Flip();
         }
         else if (facingRight == false && horitonalMove > 0)
         {
             Flip();
         }
     }
 
     public void Flip()
     {
         facingRight = !facingRight;
         Vector2 Scaler = transform.localScale;
         if (facingRight == false)
         {
             Scaler.x = -1;
         }
         else
         {
             Scaler.x = 1;
         }
         transform.localScale = Scaler;
     }
 
     public void Jump()
     {
         rb.velocity = Vector2.up * jumpForce;
         currentJumpCount--;
     }
 
     private void JumpHigher()
     {
         if (jumpTime > 0)
         {
             rb.velocity = Vector2.up * jumpForce;
             jumpTime -= Time.deltaTime;
         } 
         else if (jumpTime <= 0)
         {
             isJumping = false;
         }
     }
     private void JumpController()
     {
         if (isGrounded == true && isJumping == false)
         {
             currentJumpCount = jumpCount;
         }
         if (isDashing == false && isJumping == false && rb.velocity.y < 0)
         {
             rb.velocity += Vector2.up * Physics2D.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
         }
     }
 
     private void Move()
     {
         rb.velocity = new Vector2(horitonalMove * runSpeed, rb.velocity.y);
     }
     public void Dash()
     {
         if (currentDashTimer <= 0)
         {
             isDashing = false;
         }
         else if (facingRight)
         {
             rb.velocity = Vector2.right * dashForce;
         }
         else if (!facingRight)
         {
             rb.velocity = Vector2.left * dashForce;
         }
         currentDashTimer -= Time.deltaTime;
     }
     
     public void DashGravityController()
     {
         if (isDashing)
         {
             rb.gravityScale = 0;
         }
         else
         {
             rb.gravityScale = initialGravity;
         }
     }
 
     public void SetCameraOffset()
     {
         if (horitonalMove > 0)
         {
             CameraFollow.goingRight = true;
         }
         else if (horitonalMove < 0)
         {
             CameraFollow.goingRight = false;
         }
     }
 
     private void OnDrawGizmos()
     {
         Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius);
     }
 
 
 }

I have already tried a lot of things but nothing worked. Like moving dash to FixedUpdate, setting gravity to 0 before dash, even freezing y axis.

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

165 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

Related Questions

Creating a Trampoline type object? 1 Answer

Why is my momentum not carrying over to the X axis? 0 Answers

How do I make a 2D player controlled ball roll when moving? 1 Answer

[Solved] Zero out Velocity before double jumping? 0 Answers

Bounce not working when Velocity is below 1f , what could it be? 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