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 President · Sep 24, 2020 at 01:02 PM · 3dcharactercontrollerjump

Character doesn't jump sometimes

Hello. I am using the following CharacterController and everything is working well, except that sometimes the character jumps and sometimes it doesn't when I hit the spacebar. What is wrong here?

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(CharacterController))]
 public class RPGMovement : MonoBehaviour
 {
     public float ForwardSpeed = 8f;
     public float BackwardSpeed = 4f;
     public float StrafeSpeed = 5f;
     public float RotateSpeed = 110f;
 
     private CharacterController m_CharacterController;
     Vector3 m_LastPosition;
     Animator m_Animator;
     PhotonView m_PhotonView;
     PhotonTransformView m_TransformView;
 
     float m_AnimatorSpeed;
     Vector3 m_CurrentMovement;
     float m_CurrentTurnSpeed;
 
     private Vector3 playerVelocity;
     private bool groundedPlayer;
     private float jumpHeight = 0.9f;
     private float gravityValue = -20.81f;
 
     void Start()
     {
         m_CharacterController = gameObject.GetComponent<CharacterController>();
         m_Animator = GetComponent<Animator>();
         m_PhotonView = GetComponent<PhotonView>();
         m_TransformView = GetComponent<PhotonTransformView>();
     }
 
     void Update()
     {
         if (m_PhotonView.isMine == true)
         {
             ResetSpeedValues();
 
             UpdateRotateMovement();
 
             UpdateForwardMovement();
             UpdateBackwardMovement();
             UpdateStrafeMovement();
             UpdateJump();
 
             MoveCharacterController();
 
 
             ApplySynchronizedValues();
         }
         UpdateAnimation();
     }
 
     void UpdateAnimation()
     {
         Vector3 movementVector = transform.position - m_LastPosition;
 
         float speed = Vector3.Dot(movementVector.normalized, transform.forward);
         float direction = Vector3.Dot(movementVector.normalized, transform.right);
 
         if (Mathf.Abs(speed) < 0.2f)
         {
             speed = 0f;
         }
 
         if (speed > 0.6f)
         {
             speed = 1f;
             direction = 0f;
         }
 
         if (speed >= 0f)
         {
             if (Mathf.Abs(direction) > 0.7f)
             {
                 speed = 1f;
             }
         }
 
         m_AnimatorSpeed = Mathf.MoveTowards(m_AnimatorSpeed, speed, Time.deltaTime * 5f);
 
         m_Animator.SetFloat("Speed", m_AnimatorSpeed);
         m_Animator.SetFloat("Direction", direction);
 
         m_LastPosition = transform.position;
     }
 
     void ResetSpeedValues()
     {
         m_CurrentMovement = Vector3.zero;
         m_CurrentTurnSpeed = 0;
     }
 
     void ApplySynchronizedValues()
     {
         m_TransformView.SetSynchronizedValues(m_CurrentMovement, m_CurrentTurnSpeed);
     }
 
     void MoveCharacterController()
     {
         m_CharacterController.Move(m_CurrentMovement * Time.deltaTime);
     }
 
     void UpdateForwardMovement()
     {
         if (Input.GetKey(KeyCode.W) || Input.GetAxisRaw("Vertical") > 0.1f)
         {
             m_CurrentMovement = transform.forward * ForwardSpeed;
         }
     }
 
     void UpdateBackwardMovement()
     {
         if (Input.GetKey(KeyCode.S) || Input.GetAxisRaw("Vertical") < -0.1f)
         {
             m_CurrentMovement = -transform.forward * BackwardSpeed;
         }
     }
 
     void UpdateStrafeMovement()
     {
         if (Input.GetKey(KeyCode.Q) == true)
         {
             m_CurrentMovement = -transform.right * StrafeSpeed;
         }
 
         if (Input.GetKey(KeyCode.E) == true)
         {
             m_CurrentMovement = transform.right * StrafeSpeed;
         }
     }
 
     void UpdateRotateMovement()
     {
         if (Input.GetKey(KeyCode.A) || Input.GetAxisRaw("Horizontal") < -0.1f)
         {
             m_CurrentTurnSpeed = -RotateSpeed;
             transform.Rotate(0.0f, -RotateSpeed * Time.deltaTime, 0.0f);
         }
 
         if (Input.GetKey(KeyCode.D) || Input.GetAxisRaw("Horizontal") > 0.1f)
         {
             m_CurrentTurnSpeed = RotateSpeed;
             transform.Rotate(0.0f, RotateSpeed * Time.deltaTime, 0.0f);
         }
     }
 
     void UpdateJump()
     {
         groundedPlayer = m_CharacterController.isGrounded;
         if (groundedPlayer && playerVelocity.y < 0)
         {
             playerVelocity.y = 0f;
         }
 
         // Changes the height position of the player..
         if (Input.GetButtonDown("Jump") && groundedPlayer)
         {
             playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
             m_Animator.SetTrigger("Jump");
         }
 
         playerVelocity.y += gravityValue * Time.deltaTime;
         m_CharacterController.Move(playerVelocity * Time.deltaTime);
     }
 }
Comment
Add comment · Show 5
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 rh_galaxy · Sep 24, 2020 at 01:16 PM 0
Share

How do you update m_CharacterController.isGrounded?

avatar image President rh_galaxy · Sep 24, 2020 at 01:21 PM 0
Share

I am new to Unity, and since you asked this question, I understand that "m_CharacterController.isGrounded" needs to be updated, but I think I am not doing that. How can I do it?

avatar image rh_galaxy President · Sep 24, 2020 at 01:30 PM 0
Share

I'm not at all sure, but depending on how it is set, you might miss the one frame that GetButtonDown is true for when you press the space bar... You can debug it by adding something like this after the first few lines in UpdateJump().

 if(Input.GetButtonDown("Jump")) Debug.Log("Jumping, groundedPlayer is " + groundedPlayer?"true":"false");

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by unity_Z4478tN91mRAQQ · Sep 24, 2020 at 03:13 PM

You can try to put a Debug.Log() in your jump method to check if the problem come from that part or from before.

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 President · Sep 24, 2020 at 10:17 PM 0
Share

$$anonymous$$y player is grounded for sure, I did debug using raycast and it confirmed it. But problem still happening. Could it be my animator?

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

199 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

Related Questions

Jumping not always work 2 Answers

How to make a ball jump? 2 Answers

I aren't able to jump. I find no mistake in the script 1 Answer

How to jump multiple times to mid air? 3 Answers

I cant make my Character jump. 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