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 DemonJAZ · Jun 09, 2019 at 06:55 AM · scripting problem2d game2d-platformer2d-physics

Jumping isn't smooth while going up

So i am trying to implement my custom physics on 2d sprite. the upward translation isn't smooth , however coming down is smooth in my player controller.cs.

Project link : https://github.com/DemonJAZ/WalkInForest2DGame

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class PlayerController : MonoBehaviour { private bool grounded = false; public float gravitymodifier = 1f; public float jumpforce; public float movementSpeed; private Rigidbody2D Pico; private float inputX,inputY;

 // Start is called before the first frame update
 void Start()
 {
     Pico = gameObject.GetComponent<Rigidbody2D>();
 }

 private void Update()
 {
     if (Input.GetKeyDown(KeyCode.Space) && grounded)
     {

         transform.Translate(Vector2.up * jumpforce * Time.deltaTime);
         grounded = false;

     }

 }

 // Update is called once per frame
 private void FixedUpdate()
 {
     if (Input.GetKey(KeyCode.D))
     {
         transform.Translate(Vector2.right * movementSpeed * Time.deltaTime);
     }
     if (Input.GetKey(KeyCode.A))
     {
         transform.Translate(Vector2.left * movementSpeed * Time.deltaTime);
     }
     if (!grounded)
     {
         transform.Translate(Physics2D.gravity * gravitymodifier * Time.deltaTime);
     }

     if (Physics2D.Raycast((Vector2)transform.position + (Vector2.down * 0.7f) , Vector2.down,0.1f) && !grounded)
     { 
         grounded = true;
     }
 }

}

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 highpockets · Jun 09, 2019 at 10:54 AM

Well, when you press space and you are grounded, you apply the jumpforce in the world up direction using translate, but on the same frame isGrounded becomes false and in the next fixed update you are passing the gravity vector to the translate method which is overriding your jump. You need to account for your jump while you are jumping in the FixedUpdate() while isGrounded is false, but a rigidbody does this for you if you use it correctly


I see you have a Rigidbody2D component, but you are not using it in your movement logic which seems a bit odd since you assign it to a variable named Pico, yet you never do anything with Pico. Is it a kinematic rigidbody??


If you want to use the rigidbody in a way that calculates the physics for you, it needs to be dynamic and you need to AddForce() for movement or directly change the velocity vector (changing the velocity vector directly could lead to unexpected behavior unless you know what you are doing) and AddTorque() for rotations or directly change the angular velocity vector (once again, directly changing the angular velocity is not recommended for the same reasons as above). While directly modifying the velocity vector is not recommended, by looking at your code, it seems that you are setting exact values for movement (kind of arcade style) and if that is what you want instead of gradually speeding up for example, modifying the velocity vector of the rigidbody might be exactly what you are looking for.


 bool startingToJump = false;;
 
 private void Update()
  {
      if (Input.GetKeyDown(KeyCode.Space) && grounded)
      {
             startingToJump = true;
      }
  }
 
  // Update is called once per frame
  private void FixedUpdate()
  {
      float xVel = 0.0f;
      float yVel = 0.0f;
 
      if (Input.GetKey(KeyCode.D))
      {
          xVel = (1 * movementSpeed * Time.deltaTime);
      }
      else if (Input.GetKey(KeyCode.A))
      {
          xVel = (-1 * movementSpeed * Time.deltaTime);
      }
      if( startingToJump)
     {
          yVel = (1 * jumpforce);
          startingToJump = false;
     }
      if (Physics2D.Raycast((Vector2)transform.position + (Vector2.down * 0.7f) , Vector2.down,0.1f) && !grounded)
      { 
          grounded = true;
      }
      else
      {
          grounded = false;
      }
      Pico.velocity = new Vector2(xVel,  Pico.velocity.y + yVel);
  }


That is a bit of untested code, but I think it might be what you are looking for. If you really wanted to calculate the physics without the help of the rigidbody that will be more code. I took out the gravity code because the rigidbody does that for you. Hope that helps, cheers

Comment
Add comment · Show 6 · 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 DemonJAZ · Jun 09, 2019 at 11:12 AM 0
Share

Thanks for the you solution , but i am trying to implement in kinematic scenario, my Rigidbody is kinematic.

avatar image highpockets DemonJAZ · Jun 09, 2019 at 12:52 PM 0
Share

In that case you need to save your manual jump velocity for example and add gravity every frame to change it over time. Below is some untested code


 Vector2 vel; 
 
 // Start is called before the first frame update
  void Start()
  {
      Pico = gameObject.GetComponent<Rigidbody2D>();
  }
 
  private void Update()
  {
      if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space) && grounded)
      {
          vel.y =  jumpforce * Time.deltaTime;
          transform.Translate(vel);    
          grounded = false;
 
      }
 
  }
 
  // Update is called once per frame
  private void FixedUpdate()
  {
      if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.D))
      {
          vel.x = movementSpeed * Time.deltaTime;
      }
      if else (Input.Get$$anonymous$$ey($$anonymous$$eyCode.A))
      {
           vel.x = -movementSpeed * Time.deltaTime;
      }
      else
      {
           vel.x = 0.0f;
      }
      if (!grounded)
      {
          vel.y = vel.y + (Physics2D.gravity.y * gravitymodifier * Time.deltaTime);
      }
      if (Physics2D.Raycast((Vector2)transform.position + (Vector2.down * 0.7f) , Vector2.down,0.1f) && !grounded && vel.y <= 0.0f)
      { 
          grounded = true;
          vel.y = 0.0f;
      }
      transform.Translate(vel);
  }
 }


avatar image DemonJAZ highpockets · Jun 09, 2019 at 01:08 PM 0
Share

Thank you so much, i think i was focusing more on overco$$anonymous$$g the gravity rather than stopping it and focus more on implementing velocity upwards to Y axis.

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

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

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

Problem with 2D movment system C#. Keeps moving when no command is given 0 Answers

Way to achieve 3 lane mechanism in a 2D game 0 Answers

HOLD JUMP BUTTON TO JUMP HIGHER 2 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