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 Viredae · Mar 15, 2018 at 07:46 PM · 2d game2d-platformerrigidbody2d2d-physics

2D Character won't jump diagonally

So I've been making a character controller with the jump and dash mechanic, both work fine, but when I try and make the character jump while dashing (and maintaining the velocity along the horizontal axis), the character simply jumps upwards, with no forward velocity, here's the jump code involved:

if (Input.GetButtonDown("Jump") && groundCheck) { if (Dashing == true) {

              if (facingRight)
              {
                  Debug.Log("DASH JUMP");
                  rb2d.velocity = new Vector2(rb2d.velocity.x + dashSpeed * 1, rb2d.velocity.y + jumpTakeOffSpeed);
              } 
              else
                  rb2d.velocity = new Vector2(rb2d.velocity.x + dashSpeed * -1, rb2d.velocity.y + jumpTakeOffSpeed);
          }   
          else
              rb2d.velocity = new Vector2(rb2d.velocity.x, rb2d.velocity.y + jumpTakeOffSpeed);
 
          timeStamp = Time.time;
          canMove = true;
      }

And here is the code for the dash:

      if (groundCheck && Input.GetButtonDown ("Dash"))
      {  
              Dashing = true;
              timeStamp = Time.time + dashtime;
 
              if (facingRight)
                  rb2d.velocity = new Vector2(dashSpeed * 1, 0);
              else
                  rb2d.velocity = new Vector2(dashSpeed * -1, 0);
 
              canMove = false;
      }
 
      if (Dashing && timeStamp <= Time.time) 
      {
          Dashing = false;
          canMove = true;
      }

Simply walking and then pressing the jump button works just fine and the character will jump diagonally there, this is only a problem when I try to jump during the dash.

Is there any reason for that character to come to a stop complete stop, and then jump upwards instead of jumping diagonally?

P.S. "canMove" and "timeStamp" are related to the dash, the first is to stop the player from messing with the velocity using the directional buttons, and timeStamp is there to time the dash.

I'm trying to see if there's a variable or conditional that I forgot about that's forcing it to stop dashing before jumping, but so far there's nothing, I'll mention that if I find something.

Comment
Add comment · Show 3
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 hrishihawk · Mar 17, 2018 at 03:50 AM 0
Share

@Viredae Could you get rid of dashSpeed * 1 and dashSpeed * -1 from your jump script and give it a try ?

avatar image Viredae hrishihawk · Mar 17, 2018 at 08:25 PM 0
Share

Hey, sorry for not responding earlier, I tried removing those, no difference.

avatar image hrishihawk · Mar 17, 2018 at 07:35 AM 0
Share

@Viredae were you able to fix it ?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Maritto7 · Mar 17, 2018 at 06:06 AM

You are adding the dash speed twice and nullifying that as well. On the dash code try changing this line:

   if (groundCheck && Input.GetButtonDown ("Dash"))
   {  
           Dashing = true;
           timeStamp = Time.time + dashtime;
 
           if (facingRight)
               rb2d.velocity = new Vector2((rb2d.velocity.x + dashSpeed), rb2d.velocity.y); //Here you were making Y speed and X speed 0 and then adding the dash speed.
           else
               rb2d.velocity = new Vector2((rb2d.velocity.x + dashSpeed) * -1, rb2d.velocity.y);//Here you were making Y speed and X speed 0 and then adding the dash speed.
 
           canMove = false;
   }
 
   if (Dashing && timeStamp <= Time.time) 
   {
       Dashing = false;
       canMove = true;
   }


Also, you should avoid setting velocity's directly, that can cause weird behaviors like stopping mid air and then regaining momentum. As you were doing. Try using the methods implemented by unity to prevent that.

Comment
Add comment · Show 2 · 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 Viredae · Mar 17, 2018 at 08:19 PM 0
Share

Hey, thanks for responding. I'm kinda new to coding on Unity, so I'm not sure what methods you're talking about, I've been working from a lot of old tutorials because a lot of the official ones are pretty generic.

avatar image Maritto7 Viredae · Mar 18, 2018 at 01:55 AM 0
Share

If there is something I love about unity is they have an awesome documentation. Here is a link for unity rigidbody methods and properties: https://docs.unity3d.com/ScriptReference/Rigidbody.html

And we have all been there, just read and practice a lot.

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

89 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

Related Questions

,Hey! I'm trying to make a Dash move like Celeste but my Dash doesn't seem to work Horizontally(X Axis), My Dash is working perfectly fine Upward and Downwards(Y Axis). 1 Answer

I need to shoot a projectile in my 2D platformer in the opposite direction my player is dashing which can be in any direction the player points the joystick 2 Answers

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

Unity crashes 0 Answers

Trying to get actual good arrow physics in 2D 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