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 unity_2_3_VXKGFGtKtg · Sep 24, 2019 at 12:48 PM · c#physicsprogrammingjumpingboxcollider

[i know it has been asked but my problem is different can't find answers for that] Help With Ground Detection, Using BoxCollider Raycast method not working like i want

hello guys, i am trying to make my character jump which is basically a box right now jump is working but if i keep pressing space key character keeps jumping in mid air i don't want that. i know it can be solved by ground detection i am RayCast for that, problem with that is if ground is uneven ( slop like) jump stops working because of that i need a efficient way for detecting ground. there is a thing "CheckBox" i don't know how to use it i would be great if anyone tell how can i detect ground using it and explain a bit. please help and thank you in advance

My Code for now

 public class PlayerJump : MonoBehaviour
 {
 
 
     public float JumpForce;
     public float ForwardMove_Jump;
     public float distToGrorund;
     public bool Jump;
     private BoxCollider col;
     private CharacterControl characterControl;
 
     private void Awake()
     {
         characterControl = GetComponent<CharacterControl>();
         col = GetComponent<BoxCollider>();
         distToGrorund = col.bounds.extents.y;
     }
     private void Update()
     {
         if (Input.GetKeyDown(KeyCode.Space))
         {
             Jump = true;
         }
         else
         {
             Jump = false;
         }
     }
 
     private void FixedUpdate()
     {
         if (Jump == true && isGrounded())
         {
             characterControl.RIGIDBODY.velocity = transform.TransformDirection(0, JumpForce, ForwardMove_Jump) * Time.deltaTime;
         }
     }
 
     private bool isGrounded()
      {
         return Physics.Raycast(transform.position, -Vector3.up, distToGrorund + 0.1f);
      }
 }


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

Answer by tormentoarmagedoom · Sep 24, 2019 at 02:49 PM

Hello there!

Most easy solution is to tag all ground objects as "ground". Then create a box collider (lets cal itl detector) marked as trige, under your character, and use the Unity function OnTriggerStay() in this detector.

This way, you will know each frame is this Detector is colliding with other colliders. So, you only need to check if any of this other colliders is tagged "ground".

So if you have in your character FixedUpdate a command to set "grounded" bool variable to false, and then in your OnTriggerStay() a "if sentence" detecting other collider tagged as ground, make this grounded variable true.

Something like this (not code, just explainign logic)

 FixedUpdate()
 {
 Grounded = false
 }
 
 OnTriggerStay(collider other)
 {
  if (other.tag == "ground")
  {
  grounded = true
  }
 }

This way, you will know every moment if its or not grounded, so you can allow or not to jump.

There are several youtube tutorials about this. If the detector does not work well in slopes, make your detector have another size, or move it a little, or make more than 1 collider to have all "areas" covered.

Good luck!

Comment
Add comment · Show 4 · 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 unity_2_3_VXKGFGtKtg · Sep 24, 2019 at 03:25 PM 0
Share

Can you please give men link for one the video that follow this step

avatar image tormentoarmagedoom unity_2_3_VXKGFGtKtg · Sep 29, 2019 at 06:09 PM 0
Share

Look for them, spend your time. If pretend to learn quick ,you will learn nothing. If go slowly and watching videos you will finnally learn things you didnt expected more usefull that what you where looking for.

good luck my friend! :D

avatar image unity_2_3_VXKGFGtKtg tormentoarmagedoom · Sep 30, 2019 at 04:33 AM 0
Share

i was looking for the solution from more than a week now can't find anything, ,but thank you i searched your way alot and found a question on this site and fixed it

Show more comments

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

223 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

Related Questions

Multiple Cars not working 1 Answer

I'm having troubles using a Capsule Cast 2 Answers

how to apply force on correct direction in wall jump using Rigidbody 2 Answers

How to convert this to up and down wall run? 0 Answers

Tightening up turning radius with AddRelativeForce (or other options) 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