• 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 /
  • Help Room /
avatar image
0
Question by AlphaPrimeG · Nov 24, 2021 at 11:45 AM · collisionphysicsplayer movementgame developmentrigidbody physics

Player jumps randomly after a jump

Aways when I try to code a player controller. I face some weird issues, one or then is when the player jumps and hit the ground and the other is different heights when jump is pressed, sometimes he jumps again automatically. He does it after a collision with cubes. I tried to reset the rigidbody velocity in the jump method setting the Y velocity of the rigidbody to zero and the jump height difference apparently was fixed, but the player keeps doing some weird jumps after getting grounded. I tried to use checksphere, one raycast, two, etc. But nothing fixes this glitch. I’m using the new input system and I tried all collision detection types of the rigidbory and all interpolation options. Thanks for all!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.InputSystem;
 
 public class PlayerController : MonoBehaviour
 {
     Vector2 movementInput;
     Vector3 moveDirection;
     
     Rigidbody rb;
     Animator animator;
 
     [SerializeField]Transform groundCheckPosition;
     [SerializeField] Transform groundCheckPositionTwo;
     [SerializeField]float groundCheckRadius;
     [SerializeField] LayerMask ground;
 
     [SerializeField] float moveSpeed;
     [SerializeField] float jumpForce;
     
     float jump;
 
     bool _isMovementPressed;
     bool isJumpPressed;
     bool isWalking = false;
     bool isGrounded;
 
     int runningHash;
     int jumpingHash;
 
     [SerializeField] float groundCheckDistance;
     
     public float fallMultiplier = 1f;
     [SerializeField] Transform[] groundDetectors;
     // Start is called before the first frame update
     private void Awake()
     {
         rb = GetComponent<Rigidbody>();
         animator = GetComponent<Animator>();
 
         runningHash = Animator.StringToHash("isRunning");
         jumpingHash = Animator.StringToHash("isJumping");
 
     }
 
     // Update is called once per frame
     void Update()
     {
         isGrounded = RaycastFromAllGroundDetectors();
         //Move the player(Input)
         //moveDirection = Vector2.zero;
         moveDirection = new Vector3(movementInput.x, 0, 0);
         _isMovementPressed = movementInput.x != 0;
         //Debug.Log(_isMovementPressed);
         //Rotate the Player
         if (_isMovementPressed)
         {
             Quaternion newRotation = Quaternion.LookRotation(moveDirection);
             transform.rotation = newRotation;
         }
         if (jump > 0 && isGrounded)
         {
 
             isJumpPressed = true;
         }
         if (rb.velocity.y < 0)
         {
             rb.velocity += Vector3.up * Physics.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
 
         }
     }
 
     private void FixedUpdate()
     {
         //Vector3 gravity = globalGravity * gravityScale * Vector3.up;
         //rb.AddForce(gravity, ForceMode.Acceleration);   
         //IsGrounded();
         //GroundDetection();
 
         Move();
         Jump();
 
         if (isGrounded == false && rb.velocity.y >=1)
         {
             animator.SetBool(jumpingHash, true);
         }
         else if (isGrounded == true && !isJumpPressed)
         {
             animator.SetBool(jumpingHash, false);
 
         }
     }
 
     void Move()
     {
         isWalking = animator.GetBool(runningHash);
         rb.velocity = new Vector3(moveSpeed * movementInput.x, rb.velocity.y, 0);
         //rb.MovePosition((Vector3)transform.position + (moveDirection * moveSpeed * Time.deltaTime));
         //rb.MovePosition(transform.position + moveDirection * Time.deltaTime * moveSpeed);
         //rb.AddForce(moveDirection * moveSpeed * Time.deltaTime, ForceMode.VelocityChange);
         //rb.position = rb.position + moveDirection * moveSpeed * Time.fixedDeltaTime;
         if (_isMovementPressed && !isWalking)
         {
             animator.SetBool(runningHash, true);
         }
         else if (!_isMovementPressed && isWalking)
         {
             animator.SetBool(runningHash, false);
         }
     }
 
     void Jump()
     {
         //if (rb.velocity.y < 0)
         //{
         //    rb.velocity += Vector3.up * Physics.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
         //}
         //else if (rb.velocity.y < 0 && jump < 0)
         //{
         //    rb.velocity += Vector3.up * Physics.gravity.y * (lowJumpMultiplier - 1) * Time.deltaTime;
         //}
 
         if (isJumpPressed && isGrounded)
         {
             isGrounded = false;
             //rb.AddForce(Physics.gravity * (gravityScale - 1) * rb.mass);
             //rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
 
             //rb.AddForce(Vector3.up * jumpForce);
             rb.AddForce(new Vector3(0, jumpForce, 0), ForceMode.Impulse); 
             //rb.AddForce(new Vector3(0, jumpForce * (gravityScale - 1) * rb.mass, 0) , ForceMode.Impulse);
             //rb.velocity = Vector3.up * jumpForce;
             //animator.SetBool(jumpingHash, isGrounded);
             isJumpPressed = false;
             rb.velocity = new Vector3(0, 0, 0);
         }
         
     }
 
     public void OnMove(InputAction.CallbackContext context)
     {
         movementInput = context.ReadValue<Vector2>();
         //Debug.Log(movementInput);
     }
 
     public void OnJump(InputAction.CallbackContext context)
     {
         jump = context.ReadValue<float>();
         //isJumpPressed = true;
     }
 
     //void IsGrounded()
     //{
 
     //    if (Physics.CheckSphere(groundCheckPosition.position, groundCheckRadius, ground))
     //    {
     //        isGrounded = true;
     //    }
     //    else
     //    {
     //        isGrounded = false;
     //    }
     //    //if (Physics.CheckSphere(groundCheckPositionTwo.position, groundCheckRadius, ground))
     //    //{
     //    //    isGrounded = true;
     //    //}
     //    //else
     //    //{
     //    //    isGrounded = false;
     //    //}
 
     //    //var hitColliders = Physics.OverlapSphere(groundCheckPositionTwo.position, groundCheckRadius, ground);
     //    //isGrounded = hitColliders.Length > 0;
 
 
     //    animator.SetBool(jumpingHash, !isGrounded);
         
     //}
 
     bool GroundDetection(Transform groundDetector)
     {
         
         Vector3 position = groundDetector.position;
         Vector3 up = groundDetector.up;
 
         if (Physics.Raycast(position, -up, groundCheckDistance, ground))
         {
             
             Debug.DrawRay(position, -up * groundCheckDistance, Color.yellow);
             Debug.Log("Hit");
             return true;
         }
         else
         {
             Debug.DrawRay(position, -up * groundCheckDistance, Color.white);
             Debug.Log("No Hit");
             
         }
 
         //if (Physics.Raycast(transform.position, transform.TransformDirection(-Vector3.up), groundCheckDistance, ground))
         //{
         //    Debug.DrawRay(transform.position, transform.TransformDirection(-Vector3.up) * groundCheckDistance, Color.yellow);
         //    Debug.Log("Hit");
         //}
         //else
         //{
         //    Debug.DrawRay(transform.position, transform.TransformDirection(-Vector3.up) * groundCheckDistance, Color.white);
         //    Debug.Log("No Hit");
         //}
         return false;
     }
     bool RaycastFromAllGroundDetectors()
     {
         foreach (var groundDetector in groundDetectors)
         {
             if (GroundDetection(groundDetector)) return true;
         }
         return false;
     }
     
 }
 
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 MilitaryG · Nov 24, 2021 at 12:55 PM 0
Share

As far as I understand you have drag set to 0 in rigidbody component

I had same issues on colliding.

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Floor is made up of tiles with their own box colliders. Pushable item gets stuck on the "seams" 0 Answers

Collision Between Two Child GameObjects 0 Answers

Rigidbody randomly going through collider 1 Answer

(Pool/8Ball Game) Strange Elastic Collisions 1 Answer

Trying to figure out how to properly use my CircleCast Detection in my 2D Prototype 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