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 /
avatar image
0
Question by PinkLad45 · Jul 21, 2020 at 05:15 PM · movementcontrollerjumpaddforceteleport

Player appears to teleport instead of adding force

I tried to add a simple jumping mechanic, The player does seem "jump", But instead of going up smoothly it just teleports up, Here's the code, Any help is appreciated.

 public class PlayerMovement : MonoBehaviour
 {
     public Rigidbody2D RigidBody;
     Vector2 Movement;
     public float Speed = 10f;
     public Animator Anim;
     public float JumpForce;
     public bool IsGrounded;
     public Transform GroundCheck;
     public float CheckRadius;
     public LayerMask Ground;
 
     void Start()
     {
         RigidBody = GetComponent<Rigidbody2D>();
     }
     void Update()
     {
         Movement.x = Input.GetAxisRaw("Horizontal");
         Anim.SetFloat("Horizontal", Movement.x);
         Anim.SetFloat("Speed", Movement.sqrMagnitude);
         if (Movement.x > 0) {
         transform.localScale = new Vector2 (10.0f, 10.0f);
         } else if (Movement.x < 0) {
         transform.localScale = new Vector2 (-10.0f, 10.0f); 
         }
         if (Input.GetButtonDown("Jump") && IsGrounded == true) {
         RigidBody.AddForce(transform.up * Time.deltaTime * JumpForce); //Here is the thing.
         }
     }
     void FixedUpdate()
     {
         IsGrounded = Physics2D.OverlapCircle(GroundCheck.position, CheckRadius, Ground);
         RigidBody.MovePosition(RigidBody.position + Movement * Speed * Time.fixedDeltaTime);
         if (Input.GetButtonDown("Jump") && IsGrounded == true) {
         RigidBody.AddForce(transform.up * JumpForce);
         }
     }
 }
 
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by N-8-D-e-v · Jul 21, 2020 at 05:46 PM

You add force twice, once in update, and once in fixed update, this is probably causing your problems. FixedUpdate is called every physics step, so you want to use fixedupdate for using rigidbody/physics related things.

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 PinkLad45 · Jul 21, 2020 at 06:05 PM 0
Share

Crap, How didn't I notice that

avatar image N-8-D-e-v PinkLad45 · Jul 26, 2020 at 01:54 AM 0
Share

:) make sure to mark a correct answer

avatar image
0

Answer by davidcox70 · Jul 21, 2020 at 05:54 PM

Apart from being added twice as @N8- pointed out, the addition in FixedUpdate has not been multiplied by time.deltaTime. For a jump, you don't really need to have this multiple, because a jump height is not dependent on frame rate. However, since time.deltaTime is likely to be a low fraction like 1/60, not having it in your fixed update section means you are actually adding a huge amount more force than perhaps you are expecting. DC

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 Eno-Khaon · Jul 21, 2020 at 08:11 PM 0
Share

$$anonymous$$ore importantly, Time.deltaTime shouldn't be multiplied into either of those regardless.

First of all, it already is when using the default Force$$anonymous$$ode for Rigidbody(2D).AddForce().

Second, an instantaneous force (such as jumping) should be applied at its full strength:

 // Apply an immediate force using Force$$anonymous$$ode2D.Impulse
 // $$anonymous$$ultiply by the Rigidbody's mass to guarantee a fixed impulse
 rb.AddForce(transform.up * jumpForce * rb.mass, Force$$anonymous$$ode2D.Impulse);


Third, "Rigidbody" is actually a pretty terrible name for your Rigidbody2D variable, since it conflicts with the 3D Rigidbody class name. (It doesn't mean DON'T use it, but that it would make things easier on you if you make a 3D game later)

Anyway, if you defined your jumping force based on putting it in Update(), then you'll want to divide the value by ~3000 (50 physics updates per second, 60 frames per second?) to get it into the range you're expecting.

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

205 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

AddForce Jump Doesn't Work 1 Answer

Jump working in editor runtime but not working in WebGL build 1 Answer

Need Scripts for wall forward to behind and climb 0 Answers

AddForce Movement Constraint 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