Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 moksh115 · Jul 03, 2017 at 06:44 AM · movementphysicsmovement scriptplatformer

Jump only sometimes works and player doesn't always jump high?

My script is supposed to be making sure that my player can jump whenever it touches the floor, however it doesn't always jump when it's touching the floor. Also, sometimes when it jumps, it jumps and sometimes it jumps really low. Can anyone help fix this problem? I really want my player to be able to jump consistently.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 
 public class Ball_Movement : MonoBehaviour {
 
     public string moveHorizontal = "Horizontal";
     public float moveSpeed;
    // public float runSpeed;
     public float walkSpeed;
     public float jump;
     public int jumpTime;
 
     public float rotateSpeed = 10f;
     
     public bool onFloor = true;
 
     Rigidbody rb;
 //   public float maxJumpHeight;
 
     // Use this for initialization
     void Start () {
         rb = GetComponent<Rigidbody>();
     //    maxJumpHeight = 2f;
        
     }
 
     // Update is called once per frame
     void Update() {
         if (Input.GetKey(KeyCode.LeftShift)) {
             moveSpeed = walkSpeed * 3;
         }
         else {
             moveSpeed = walkSpeed;
         }
 
       /*  if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
         {
             transform.Rotate(rotateSpeed, 0, 0);
         } */
 
         float lrAxis = Input.GetAxis(moveHorizontal);
         if (lrAxis != 0.0f) {
             rb.MovePosition(rb.position + rb.rotation * Vector3.right * moveSpeed * Time.fixedDeltaTime * lrAxis);
             transform.Rotate(rotateSpeed, 0, 0);
         }
 
         if (onFloor = true && (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow)))
         {
             if (jumpTime != 2)
             {
                 rb.AddForce(transform.up * jump, ForceMode.Impulse);
                 jumpTime += 1;
             }
             else
             {
                 onFloor = false;
             }
         }
           // if (rb.velocity.y >= maxJumpHeight) {
            //    rb.velocity = Vector3.zero;
             //  }
             }
         
     
 
     void OnCollisionEnter(Collision other)
     {
          if (other.transform.CompareTag("Floor"))
          {
              onFloor = true;
             jumpTime = 0;
          }
 
         if (other.transform.CompareTag("Respawn")) {
             SceneManager.LoadScene(0);
         }
      } 
 
 }
 
Comment
Add comment · Show 1
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 moksh115 · Jul 03, 2017 at 06:48 AM 0
Share

I've also commented out some bits which set a max jump limit because they don't work so if anyone could help with that it would be great!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by hexagonius · Jul 03, 2017 at 07:34 AM

Don't do MovePosition on a non kinematic rigidbody and don't use a separate AddForce. The best way to do this is to combine your movement into a single vector and assign that to the rb.velocity. But most of all, do either of any of the ways you do it in FixedUpdate. That's where Physics Updates need to be performed.

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 moksh115 · Jul 03, 2017 at 07:59 AM 0
Share

Thanks for that! Any tips on how to set a jump height limit though?

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

124 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

Related Questions

2D 360 degress platformer example needed 0 Answers

Non slippery movement 1 Answer

How can I add an impulse force on top of my walking force? 1 Answer

How Time.deltaTime affects movement ? 4 Answers

Quake Movement in Unity 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