• 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 renren30 · Apr 05, 2020 at 05:13 AM · 2d game2d-platformer2d-physics2d-gameplay

"Dash-Move" only work some times.

I have a problem with my dash move. It just work some times. Mby 1 out of 3 times. I also found out that it works more often if i press the jump key (SPACE) at the same time. Does it colide with the jump code? Any ideas how to fix it?

I marked the "dash code" with //Dash. I hope u can read the code im new in coding :D

Here is the code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerControl : MonoBehaviour
 {
     private Rigidbody2D rb;
     public float speed;
     public float jumpForce;
     private float moveInput;
 
     private bool isGrounded;
     public Transform feetPos;
     public float checkRadius;
     public LayerMask whatIsGround;
 
     private float jumpTimeCounter;
     public float jumpTime;
     private bool isJumping;
     private bool facingRight = true;
 
     //Dash
     public float dashSpeed = 30f;
     public float startDashTime = 0.1f;
     private float dashTime;
     private int direction;
 
 
     private void Start()
     {
         rb = GetComponent<Rigidbody2D>();
 
         //Dash
         dashTime = startDashTime;
     }
 
     private void FixedUpdate()
     {
         moveInput = Input.GetAxisRaw("Horizontal");
         rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
 
         if (facingRight == false && moveInput > 0)
         {
             Flip();
         } else if(facingRight == true && moveInput < 0)
         {
             Flip();
         }
 
         void Flip()
         {
             facingRight = !facingRight;
 
             transform.Rotate(0f, 180f, 0f);
         }
 
         //Dash
         if (direction == 0)
         {
             if (Input.GetKeyDown(KeyCode.LeftShift))
             {
                 if(moveInput < 0)
                 {
                     direction = 1;
                 } else if (moveInput > 0)
                 {
                     direction = 2;
                 }
             }
         } else
         {
             if(dashTime <= 0)
             {
                 direction = 0;
                 dashTime = startDashTime;
                 rb.velocity = Vector2.zero;
             } else
             {
                 dashTime -= Time.deltaTime;
 
                 if(direction == 1)
                 {
                     rb.velocity = Vector2.left * dashSpeed;
                 } else if (direction == 2)
                 {
                     rb.velocity = Vector2.right * dashSpeed;
                 }
             }
         }
         //Dash End
     }
 
     
 
     private void Update()
     {
         isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);
 
         if(isGrounded == true && Input.GetKeyDown(KeyCode.Space))
         {
             isJumping = true;
             jumpTimeCounter = jumpTime;
             rb.velocity = Vector2.up * jumpForce;
         }
 
         if (Input.GetKey(KeyCode.Space) && isJumping == true)
         {
             if(jumpTimeCounter > 0)
             {
                 rb.velocity = Vector2.up * jumpForce;
                 jumpTimeCounter -= Time.deltaTime;
             } else {
                 isJumping = false;
             }
 
         }
 
         if (Input.GetKeyUp(KeyCode.Space))
         {
             isJumping = false;
         }
 
     }
 
 }
 


Thank you.

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 dougantor · Apr 05, 2020 at 10:03 AM

Player input is best handled in the update rather than fixedupdate, try moving everything in fixedupdate to update and that should help

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 renren30 · Apr 05, 2020 at 07:31 PM 0
Share

Thank you! It worked.

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

168 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

Related Questions

Player getting stuck between grounds 0 Answers

OnCollisonEnter2D. 1 Answer

How to create soil or sand?? 1 Answer

Death Counter dont work when changing scenes 2 Answers

Having some stuck issues on the 2D infinite runner 0 Answers

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