• 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 Hadi563 · Sep 18, 2022 at 05:45 PM · jumpingfirst-person

How can I prevent the character from jumping if there is an object above them?

I'm creating t$$anonymous$$s first person game, the character can crouch and jump, when you're crouc$$anonymous$$ng and there's an object above you, if you try to uncrouch not$$anonymous$$ng happens, I do t$$anonymous$$s using a raycast upwards to check if $$anonymous$$ts somet$$anonymous$$ng, now the problem is i'm using the exact same method with jumping but it's not working, if you jump and there's an object above you, the character clips through and gets stuck. Idk if the problem is in the code or the player jumps too fast the object doesn't recognise it and the player gets stuck.Here's my code.

 private void HandleJumping()
     {
         if (shouldJump && !Physics.Raycast(characterController.transform.position, Vector3.up, 0.5f))
         {
             moveDirection.y = jumpForce;
         }            
     }
 private void HandleCrouc$$anonymous$$ng()
     {
         if (shouldCrouch)
             StartCoroutine(CrouchStand());
         if (isCrouc$$anonymous$$ng && shouldJump && !Physics.Raycast(characterController.transform.position, Vector3.up, 0.5f))
             BreakCrouchOnJump();
     }
     private void BreakCrouchOnJump()
     {
         characterController.height = standingHeight;
         characterController.center = standingCenter;
         isCrouc$$anonymous$$ng = false;
     }
     private IEnumerator CrouchStand()
     {
         if (isCrouc$$anonymous$$ng && Physics.Raycast(playerCamera.transform.position, Vector3.up, 0.5f))
             yield break;
 
         duringCrouc$$anonymous$$ngAnimation = true;
 
         float timeElapsed = 0;
         float targetHeight = isCrouc$$anonymous$$ng ? standingHeight : crouc$$anonymous$$ngHeight;
         float currentHeight = characterController.height;
         Vector3 targetCenter = isCrouc$$anonymous$$ng ? standingCenter : crouc$$anonymous$$ngCenter;
         Vector3 currentCenter = characterController.center;
 
         w$$anonymous$$le (timeElapsed < timeToCrouch)
         {
             characterController.height = Mathf.Lerp(currentHeight, targetHeight, timeElapsed / timeToCrouch);
             characterController.center = Vector3.Lerp(currentCenter, targetCenter, timeElapsed / timeToCrouch);
             timeElapsed += Time.deltaTime;
             yield return null;
         }
 
         characterController.height = targetHeight;
         characterController.center = targetCenter;
 
         isCrouc$$anonymous$$ng = !isCrouc$$anonymous$$ng;
 
         duringCrouc$$anonymous$$ngAnimation = false;
     }

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
3
Best Answer

Answer by logicandchaos · Sep 19, 2022 at 06:48 PM

Check if there is an object above them, use a ray or other collision detection solution, then you can assign it to a bool like bool isObjectAbovePlayer; you set the result of the collision detection to the bool then in your jump you check the bool, if(isObjectAbovePlayer) return; or whatever..

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 Hadi563 · Sep 20, 2022 at 03:05 PM 0
Share

Thank you very much bro it worked, much appreciated<3!

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

145 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

Related Questions

2D character can't jump off of platform floating in water 0 Answers

Character Getting Stick on Walls, Jumping Prob in Unity 0 Answers

2D AI flipping 90 degrees Yaxis in play mode, weird hopping glitch? 0 Answers

How can I make a good jumping script? 2 Answers

Strange jumping issue 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