Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
4 captures
13 Jun 22 - 14 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 BarelyUsedDesert · Feb 06 at 02:54 PM · physics3daddforcejumping

Force in AddForce with ForceMode.Impulse insconsistent

Sometimes my player jumps relatively high, but then he jumps relatively low. I tried several methods, but none of them worked.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine.SceneManagement;
 using UnityEngine;
 
 public class PlayerController : MonoBehaviour
 {
     public Rigidbody rb;
     public GameObject cam1;
     public Transform playerBody;
     RaycastHit hit;
     RaycastHit hitGround;
 
     public float MoveForce = 10f;
     public float jumppadForce = 10f;
 
     public bool hasKey;
     public bool isJumping;
 
     private void Update()
     {
         if(Physics.Raycast(cam1.transform.position, cam1.transform.forward, out hit, 4))
         {
             if (Input.GetMouseButtonDown(0))
             {
                 switch (hit.transform.gameObject.tag)
                 {
                     case "key":
                         Destroy(hit.transform.gameObject);
                         hasKey = true;
                         break;
                     case "lock":
                         if (hasKey)
                         {
                             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
                         }
                         break;
 
                 }
             }
 
             if (Input.GetMouseButton(0))
             {
                 switch (hit.transform.gameObject.tag)
                 {
                     case "movableJumppad":
                         hit.transform.SetParent(playerBody);
                         break;
                 }
             }
 
             if (Input.GetMouseButtonUp(0))
             {
                 switch (hit.transform.gameObject.tag)
                 {
                     case "movableJumppad":
                         hit.transform.SetParent(null);
                         break;
                 }
             }
         }
 
         // part of jumping mechanic
         if(Physics.Raycast(transform.position, Vector3.down, out hitGround, 1.2f))
         {
             switch (hitGround.transform.gameObject.tag)
             {
                 case "jumppad":
                     isJumping = false;
                     break;
                 case "movableJumppad":
                     isJumping = false;
                     break;
             }
         }
         else
         {
             isJumping = true;
         }
     }
 
     // Update is called once per frame
     void FixedUpdate()
     {
         // jumping mechanic
         if (!isJumping)
         {
             rb.AddForce(jumppadForce * Vector3.up * Time.fixedDeltaTime, ForceMode.Impulse);
         }
 
         transform.Translate(Input.GetAxis("Vertical") * Time.deltaTime * MoveForce * Vector3.forward);
         transform.Translate(Input.GetAxis("Horizontal") * Time.deltaTime * MoveForce * Vector3.right);
     }
 }

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 DanielGL · Feb 06 at 03:05 PM

rb.AddForce takes in cosideration the previous force of the object, so if your player was been affected by some other force before the jump, it can changes your results a bit, for jumps instead of rb.AddForce(), try to direct set the velocity by rb.velocity = jumpForce; it will ignore any previous velocity.

Edit: you may want to do it more like that rb.velocity = new Vector3(rb.velocity.x, jumpForce, rb.velocity.z); with that you can preserve your horizontal movement.

Comment
Add comment · 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
0

Answer by Captain_Pineapple · Feb 06 at 05:47 PM

If i ready this correctly then your issue will not be solved by @DanielGL s solution.


Your force application in itself is probably correct but you do not consider that it might be applied multiple times as your condition to apply the force (`!isJumping`) is always true if a jumppad is in range of a raycast. If for some reason your player does not leave the raycast range in one fixed update you will add the force in the next frame as well which would lead to a doubled applied force.


So to solve this you should basically disable the possibility for force application for at least a few frames to make sure that you actually left the ground.

Comment
Add comment · 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

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

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

Why wont my character jump? (using rigidbody and addForce) 1 Answer

Can't fix slow jumping for character controller 3D 1 Answer

How to move while jumping? 1 Answer

Instead of rotating 180, I would like to rotate directly away from an object for a wall jump, how do I get my rotation compared to objects normal? 2 Answers

Apply A Force to Reach a Specific Height 3 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