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 Conqueror1 · May 07, 2020 at 10:32 PM · button3dplayerjump

[SOLVED] Player is jumping up too fast.

The few lines of code from Brackeys tutorial have relatively solved the jumping issue.

https://www.youtube.com/watch?v=_QajrabyTJc&list=PLXJxfWH3Df9nRh_0-MCNv13Xv78dkuMAK∈dex=15&t=1197s

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class PlayerController : MonoBehaviour
 {
     public Joystick joystick;
     public float moveSpeed = 20f;
     float rotSpeed = 90;
     float rot = 0f;
     public float gravity = -9.81f;
     public float jumpForce = 10f;
 
     public int currentProgress;
     public ProgressBar ProgressBar;
 
  Vector3 moveDir = Vector3.zero;
 
     CharacterController controller;
     Animator anim;
     Rigidbody rb;
 
     void Start()
     {
         controller = GetComponent<CharacterController> ();
         anim = GetComponent<Animator> ();
         rb = GetComponent<Rigidbody> ();
    
         currentProgress = 0;
         ProgressBar.SetProgress(0);
     }
 
     void Update()
     {
 
         //Important
         if (controller.isGrounded && moveDir.y > 0)
         {
             moveDir.y = -2f;
         }
 
         if (controller.isGrounded && Input.GetButton("Jump"))
         {
             moveDir.y = Mathf.Sqrt(jumpForce * -2f * gravity);
 
         }
         moveDir.y += gravity * Time.deltaTime;
         controller.Move(moveDir * Time.deltaTime);
   
     }
 
     // Update is called once per frame
     void FixedUpdate()
     {
         //joystick controls//
 
         if (joystick.Vertical == 0f)
         {
             anim.SetBool("isRunning", false);
             moveDir = new Vector3(0, 0, 0);
         }
         else if (joystick.Vertical >= .5f)
         {
             anim.SetBool("isRunning", true);
             moveDir = new Vector3(0, 0, 1);
             moveDir *= moveSpeed;
             moveDir = transform.TransformDirection(moveDir);
         }
 
         //rotation using joystick//
 
         rot += joystick.Horizontal * rotSpeed * Time.deltaTime;
         transform.eulerAngles = new Vector3(0, rot, 0);
 
         moveDir.y += gravity * Time.deltaTime;
         controller.Move(moveDir * Time.deltaTime);
 
  }
     //pick up objects with sound and progress bar//
     void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.CompareTag("Pickup"))
         {
             other.gameObject.SetActive(false);
 
             currentProgress += 1;
             ProgressBar.SetProgress(currentProgress);
 
             SoundManager.sndMan.PlayCollectSound();
         }
 
     }
     
     public void jumpButton()
     {
         if (controller.isGrounded)
         {
             moveDir.y = Mathf.Sqrt(jumpForce * -2f * gravity);
 
         }
         moveDir.y += gravity * Time.deltaTime;
         controller.Move(moveDir * Time.deltaTime);
 
 
     }
 
 }
 



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

Answer by GreatOscarF · May 09, 2020 at 07:41 AM

@conqueror1 This should work:

  public float jumpForce = 8.0F;
  public float gravity = 20.0F;
  private Vector3 moveDir = Vector3.zero;
  
  void Update() {
      if (controller.isGrounded && Input.GetButton("Jump")) {
          moveDir.y = jumpForce;
      }
      moveDir.y -= gravity * Time.deltaTime;
      controller.Move(moveDir * Time.deltaTime);
  }

And in your whole script I would highly recommend tweaking your gravity and jumpForce a bit more. As a standard I would try 8.0F for the jumpForce and 20.0F for the gravity and then go from there. Also the gravity should no longer be a negative number.

 using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using UnityEngine.UI;
  
  public class PlayerController : MonoBehaviour
  {
      public Joystick joystick;
      public float moveSpeed = 20f;
      float rotSpeed = 90;
      float rot = 0f;
      public float gravity = -9.81f; // This should NOT be a negative number... I would recommend going for something around 20f and then tweak it - a lot.
      public float jumpForce = 10f; // This seems good but maybe you should tweak it a little bit more - for perfection :)
  
      public int currentProgress;
      public ProgressBar ProgressBar;
  
       Vector3 moveDir = Vector3.zero;
  
      CharacterController controller;
      Animator anim;
      Rigidbody rb;
  
      void Start()
      {
          controller = GetComponent<CharacterController> ();
          anim = GetComponent<Animator> ();
          rb = GetComponent<Rigidbody> ();
     
          currentProgress = 0;
          ProgressBar.SetProgress(0);
      }
  
  
      // Update is called once per frame
      void FixedUpdate()
      {
          //joystick controls//
  
          if (joystick.Vertical == 0f)
          {
              anim.SetBool("isRunning", false);
              moveDir = new Vector3(0, 0, 0);
          }
          else if (joystick.Vertical >= .5f)
          {
              anim.SetBool("isRunning", true);
              moveDir = new Vector3(0, 0, 1);
              moveDir *= moveSpeed;
              moveDir = transform.TransformDirection(moveDir);
          }
  
          //rotation using joystick//
  
          rot += joystick.Horizontal * rotSpeed * Time.deltaTime;
          transform.eulerAngles = new Vector3(0, rot, 0);
  
          moveDir.y += gravity * Time.deltaTime;
          controller.Move(moveDir * Time.deltaTime);
  
   }
      //pick up objects with sound and progress bar//
      void OnTriggerEnter(Collider other)
      {
          if (other.gameObject.CompareTag("Pickup"))
          {
              other.gameObject.SetActive(false);
  
              currentProgress += 1;
              ProgressBar.SetProgress(currentProgress);
  
              SoundManager.sndMan.PlayCollectSound();
          }
  
      }
      
      public void jumpButton()
      {
          if (controller.isGrounded)
          {
                 moveDir.y = jumpForce;
  
          }
          moveDir.y -= gravity * Time.deltaTime;
          controller.Move(moveDir * Time.deltaTime);
  
      }
  
  }





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 Conqueror1 · May 10, 2020 at 01:04 AM 0
Share

Thank you for pointing me in the right direction, some of the suggestions worked combined with Brackey's tutorial :)

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

213 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

Related Questions

Camera follow player's rotation 3 Answers

Player movement problem 0 Answers

Virtual joystick problem issue 1 Answer

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

How do I make my character jump? 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