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 ZozeR · Dec 19, 2019 at 02:55 PM · physicsrigidbody2dvelocity

Diagonal jump results with weird motion.

What i want to achieve is the player being able to jump diagonally. using this code kinda works however player jumps more to the y-axis. instead of going x,y(15,15) it jumps more like x,y(5,15) even though xpower is same as the ypower. and if i increase the xpower to 30 or more. it starts off by going left or right incredibly fast for a brief second before even starting to go upwards.

GetAxis("Horizontal_LRUD") will return between -1,1 depending on the left right input.

 private Rigidbody2D rg2d;
 
 void Start()
 {
     rg2d = GetComponent<Rigidbody2D>();
 }
 
 void Update()
 {
     if (Input.GetAxis("Horizontal_LRUD") != 0f)
     {
         rg2d.velocity = new Vector2(Input.GetAxis("Horizontal_LRUD") * 5, rg2d.velocity.y);
     }
 
     if (Input.GetKeyDown(KeyCode.A))
     {
         int xpower = 15, ypower = 15;
 
         rg2d.velocity = new Vector2(Input.GetAxis("Horizontal_LRUD") * xpower, ypower);
     }
 }
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 lgarczyn · Dec 19, 2019 at 06:46 PM

It's pretty simple, you erase the previous horizontal velocity every frame, therefore your horizontal jump only works for one frame.

To fix this, you need to check if you are currently in the air, and if so, keep the previous horizontal velocity.


The problem will be a lack of control mid-air, which is usually not user-friendly. You can reduce that by instead using mid-air acceleration. Basically instead of replacing the previous velocity, you increment it every frame by the current input.

 velocity += Input.GetAxisRaw("Horizontal_LRUD") * airAcceleration * Time.deltaTime;

 velocity.x = Mathf.Clamp(rg2d.velocity.x, -maxAirSpeed, maxAirSpeed);

Please note that because this is in Update, you will get slightly different results depending on Framerate.

Comment
Add comment · Show 4 · 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 ZozeR · Dec 21, 2019 at 03:02 PM 1
Share

Thank you, i solved it by doing this if you have any suggestions please let me know!

     void FixedUpdate()
     {
         if (footed)
         {
             if (Input.GetAxis("Horizontal_LRUD") != 0)
                 rg2d.velocity = new Vector2(Input.GetAxis("Horizontal_LRUD") * 5, rg2d.velocity.y);
             if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.A))
             {
                 int xpower = 10, ypower = 10;
                 footed = false;
                 rg2d.velocity = new Vector2(Input.GetAxis("Horizontal_LRUD") * xpower, ypower);
             }
             else if (Input.GetAxis("Vertical_LRUD") > 0)
             {
                 footed = false;
                 rg2d.velocity = new Vector2(rg2d.velocity.x, 6);
             }
         }
         else
         {
             
             if (Input.GetAxis("Horizontal_LRUD") != 0)
             {
                 rg2d.velocity += new Vector2(Input.GetAxisRaw("Horizontal_LRUD") / 4, 0);
                 rg2d.velocity = new Vector2($$anonymous$$athf.Clamp(rg2d.velocity.x, -10, 10),rg2d.velocity.y);
             }
         }
     }
 
     private void OnCollisionEnter2D(Collision2D col)
     {
         if (col.gameObject.tag == "Ground")
         {
             Debug.Log("O$$anonymous$$");
             footed = true;
         }
     }
avatar image lgarczyn ZozeR · Dec 21, 2019 at 07:56 PM 1
Share

The correct English for "footed" would be "grounded".


You could save a lot of code by simply having a velocity var that you modify then apply:

 void FixedUpdate()
 {
     Vector2 velocity = rg2d.velocity;
     float xInput = Input.GetAxis("Horizontal_LRUD");
     float yInput = Input.GetAxis("Vertical_LRUD");
     if (footed)
     {
         if (xInput != 0f)
             velocity.x = xInput * 5;
         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.A))
         {
             int xpower = 10, ypower = 10;
             footed = false;
             velocity.Set(xInput * xpower, ypower);
         }
         else if (yInput > 0f)
         {
             footed = false;
             velocity.y = 6f;
         }
     }
     else
     {
         if (xInput != 0)
         {
             velocity.x = $$anonymous$$athf.Clamp(
                 velocity.x + xInput / 4f,
                 -10f, 10f);
         }
     }
     rg2d.velocity = velocity;
 }
 
 private void OnCollisionEnter2D(Collision2D col)
 {
     //TODO switch to a raycast based isGrounded, or the player will be able to jump when touching the ceiling or walls
     if (col.gameObject.tag == "Ground")
     {
         Debug.Log("O$$anonymous$$ entering");
         footed = true;
     }
 }

Please note that if you're using GetAxis ins$$anonymous$$d of GetAxisRaw, you switching direction rapidly might allow you to make jumps in any angle.

Also, why do you need two different types of jumps?

avatar image ZozeR lgarczyn · Dec 22, 2019 at 02:47 AM 0
Share

long jumps were problematic that's why I was just testing these three events. the real script is way too long. thank you btw for the help!

I couldn't understand the comment in the Collision part. the ground's tag is "ground". ceiling and walls are tagless.

Show more comments

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

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

NullReferenceException: UnityEngine.Rigidbody2D.get_velocity() 0 Answers

rigidbody2D velocity zero? 1 Answer

Do rigidbodies (especially 2d) have limits on their velocities? 1 Answer

move 2d character affected by physics with velocity and/or add force 2 Answers

2D Geometry dash-like ship physics 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