Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 albadalejoluca · Jan 29 at 08:49 PM · rigidbodyraycastcollidermovement scriptjumping

Raycast doesn't detect the ground when the player is on a ledge.

Hi, i'm fairly new to unity and C# programming overall and i am trying to make a basic physics based movement script but the problem i have now is that i'm trying to make my player jump by checking if he's grounded with a raycast. Here is my code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Movement : MonoBehaviour
 {
     
     private bool grounded;
     public float jumpforce;
     private float distToGround;
 
     public float thrust;
 
     public Rigidbody rb;
     public Transform PlayerCam;
     public Collider PlayerCollider;
 
     public float sensitivity;
     private float xRotation;
     private float desiredX;
 
     void Start(){
         
         rb = GetComponent<Rigidbody>();
 
         distToGround = PlayerCollider.bounds.extents.y;
         
     }
 
     void Update(){
 
         movement();
         rotation();
         jump();
         GroundCheck();
     }
 
     void movement(){
 
         if (Input.GetKey("z") && grounded == true)
         {
         rb.AddRelativeForce(Vector3.forward * thrust * Time.deltaTime);
         print("z");
 
         }
 
         if (Input.GetKey("s") && grounded == true)
         {
         rb.AddRelativeForce(Vector3.back * thrust * Time.deltaTime);
         print("s");
 
         }
         
         if (Input.GetKey("q") && grounded == true)
         {
         rb.AddRelativeForce(Vector3.left * thrust * Time.deltaTime);
         print("q");
 
         }
 
         if (Input.GetKey("d") && grounded == true)
         {
         rb.AddRelativeForce(Vector3.right * thrust * Time.deltaTime);
         print("d");
 
         }      
     }
     
     void rotation(){
         
         float mouseX = Input.GetAxis("Mouse X") * sensitivity * Time.fixedDeltaTime;
         float mouseY = Input.GetAxis("Mouse Y") * sensitivity * Time.fixedDeltaTime;
 
         Vector3 rot = transform.localRotation.eulerAngles;
 
         desiredX = rot.y + mouseX;
         xRotation -= mouseY;
 
         xRotation = Mathf.Clamp(xRotation, -90f, 90f);
 
         PlayerCam.transform.rotation = Quaternion.Euler(xRotation, desiredX, 0 );
         transform.localRotation = Quaternion.Euler(0, desiredX, 0);
     }
 
     void GroundCheck(){
 
         if (Physics.Raycast(transform.position, Vector3.down, distToGround + 0.1f)) {
 
             grounded = true;
             print("grounded");
         } else {grounded = false; print("not grounded");}
     }
 
     void jump(){
 
         if (Input.GetKeyDown("space") && grounded == true){
 
             rb.AddForce(Vector3.up * jumpforce * Time.fixedDeltaTime);
         }
     }
 }

But the problem i'm having currently is that when the player (currently a capsule) is standing on a ledge, the raycast won't detect that it's grounded because the collider isn't only in the middle. Maybe i should add more raycasts but i tought it was a good idea to ask for suggestions here. (If you have any other suggestions for my code it would also be appreciated).

this is a sreenshot of the problem: alt text

unity-answers.png (375.8 kB)
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
Best Answer

Answer by Captain_Pineapple · Jan 29 at 11:36 PM

Well this is a fairly common issue that is not that easy to solve if you want to stick to raycasts. An easier way that will give you more consistent results would be to use the OnCollision callbacks instead.

Check out this question here. The code in the question already implements the groundcheck using OnCollisionEnter and OnCollisionExit. You should be able to implement that in your own script.


Let me know if that helped.

Comment
Add comment · Show 7 · 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 albadalejoluca · Jan 30 at 12:53 PM 0
Share

Hi, thanks for responding to my question. Indeed, i saw it was also possible to do it like that but on an older question i saw someone saying that the OnCollisionExit method wasn't reliable and it's better to use raycasts instead. Is this still true or has this since been fixed ? Either way thanks for the response and i'll see if it works later today or next week.

avatar image Captain_Pineapple albadalejoluca · Jan 30 at 01:29 PM 0
Share

well both methods have their issues. You already discovered the issue with raycasts. Solving this reliably is not that easy if you want to stick to raycasts. You can do a spherecast instead which would cover the complete surface of your current collider. This has the downside that it will get inaccurate if your character collider should ever become any more complex.


Doing things with OnCollision methods is reliable but you need to keep track of how many times the callbacks where called.

Imagine the following: you move from one ground collider onto another so that you are in contact with both at some point. Unity will tell you twice: "hey, you just collided with something that is tagged as ground". But then when leaving the one collider you are still on ground but you correctly get an OnCollisionExit call. If you do not handle in your code that OnCollisionEnter was called twice you would then say: "hey, i just left ground so i am in the air. So when leaving one Collider onto anotherone would prevent you from jumping unless you stand on the gap between 2 colliders again or drop of the collider onto different ground.


So basically while the solution i pointed out in general works, you are correct that it is not always as simple as that.

good luck, let us know how it went.

avatar image albadalejoluca Captain_Pineapple · Jan 30 at 02:16 PM 0
Share

Oh , ok i understand what you mean. I'll test it out and tell you how it went when i do. Thanks a lot!

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

252 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

Related Questions

How to rotate an object 360 degrees around a sphere with collisions? 0 Answers

isGrounded is always false, even with gravity, how do you fix that? 1 Answer

How to limit a ray to go downwards to infinity, how can I make it down only 1 float. 0 Answers

attached.Rigidbody isn't working with a ChactacterController? -1 Answers

Raycast Problem. CharacterJoint/Rigibodies 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