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 /
This question was closed Jul 30, 2019 at 11:09 AM by adovehv for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by adovehv · Jul 30, 2019 at 10:49 AM · movementplayer2d-platformerjumpjumping

Problem between my player jump and player movement

i made a simple jump and movement, but i have a problem. If i comment the 75 line (in FixedUpdate) of my code "rb2d.MovePosition(rb2d.position + mov speed Time.deltaTime);" my jump work fine, but if i not coment this line, the jump not work and the player gravity is like too less, but not change in the inspector, i'm confussed, i'm a bit noob and i dont what happends. Thanks!

 public class PlayerMovement : MonoBehaviour
 {
     [Header("Movement")]
     [SerializeField] float speed = 4f;
     [SerializeField] bool canMove;
     CircleCollider2D feetColl;
 
     private Vector2 mov = new Vector2(1, 1);
 
     Animator anim;
     Rigidbody2D rb2d;
 
     [Header("Jump")]
     [Range(1,10)]
     [SerializeField] float jumpVelocity = 1f;
     public bool jumpRequest;
 
     public float fallMultiplier = 2.5f;
     public float lowJumpMultiplier = 2f;
     
 
     void Awake()
     {
         rb2d = GetComponent<Rigidbody2D>();
     }
 
     void Start()
     {
         anim = GetComponent<Animator>();
         rb2d = GetComponent<Rigidbody2D>();
         feetColl = GetComponent<CircleCollider2D>();
         canMove = true;
     }
 
     void Update()
     {
         if (canMove)
         {           
           //  Falling();
             JumpRequest();
         }
     }
     /*
     private void Falling()
     {
         if (!feetColl.IsTouchingLayers(LayerMask.GetMask("ground")))
         {
             anim.SetBool("falling", false);
         }
         else
         {
             anim.SetBool("falling", true);
         }
     }
     */
 
     private void Run()
     {
         mov = new Vector2(Input.GetAxisRaw("Horizontal"), 0f);
 
         if (mov != Vector2.zero)
         {
             anim.SetFloat("movX", mov.x);
             anim.SetBool("running", true);
         }
         else
         {
             anim.SetBool("running", false);
         }
     }
 
     void FixedUpdate()
     {
         //Movement
         rb2d.MovePosition(rb2d.position + mov * speed * Time.deltaTime);
         Run();
         Jump();
     }
     
     private void JumpRequest()
     {
         if (Input.GetButtonDown("space"))
         {
             jumpRequest = true;
         }
     }
     
     private void Jump()
     {
         
         if (jumpRequest && !feetColl.IsTouchingLayers(LayerMask.GetMask("ground")))
         {
             return;
         }
         if (jumpRequest)
         {
             rb2d.AddForce(Vector2.up * jumpVelocity, ForceMode2D.Impulse);            
             anim.SetTrigger("jump");
             Debug.Log("jump");
             jumpRequest = false;
         }
 
         if (rb2d.velocity.y < 0)
         {
             rb2d.gravityScale = fallMultiplier;
         }
         else if (rb2d.velocity.y > 0 && !Input.GetButton("space"))
         {
             rb2d.gravityScale = lowJumpMultiplier;
         }
         else
         {
             rb2d.gravityScale = 1f;
         }
     }
     
 }

















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

  • Sort: 
avatar image
0
Best Answer

Answer by Dawdlebird · Jul 30, 2019 at 11:08 AM

Your moveposition directly sets your rigidbody's position. This basicly negates any forces you apply, and your jump depends on force. If you want to use forces, you should also do your moving left and right by applying forces in either direction, or do something like setting velocity (but then keeping the velocity.y intact to keep your jump/fall happening)

Perhaps you could also store velocity.y before you apply MovePosition, and then reapply velocity.y after. Either way, I suspect that MovePosition is your culprit here. Does it also interfere with falling speed?

Additionaly: you're using Time.deltaTime in a fixedUpdate, but there is a Time.fixedDeltaTime for that.

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 adovehv · Jul 30, 2019 at 11:09 AM 0
Share

Thank so much! :)

Follow this Question

Answers Answers and Comments

188 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

Related Questions

How to prevent player from moving in air while jumping in place?? 0 Answers

How to make the player turn in the air 1 Answer

Why won't my character jump,How do I make my character jump? 2 Answers

Why do I double jump? This isn't supposed to happen... 2 Answers

I have setup a code to make a player move around and jump, but the player will not jump. Any help would be greatly appreciated.,Player wont jump 3D 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