Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 Rxanadu · Mar 25, 2016 at 10:44 AM · raycastinggroundedspherical

Determining if a spherical object is grounded using raycast

I'm making a game revolving around moving a ball around a space. My game has a jump mechanic, so I need to know when I'm grounded in order to determine when I'm able to jump. Best way to check this is to setup raycast from ball's origin and make it constantly face downwards. However, I'm running into an embarrassing issue: the ball keeps rotating, so I'm not able to find a constant downward ray to use for finding the ground.

I started making code that looks similar to this, which the object's local transform:

 bool grounded = false;
 Ray ray = new Ray(transform.position, -transform.up);
 RaycastHit hit;
 
 if(Physics.Raycast(ray, out hit)){
 //determine if object is on ground
 grounded = true;
 }

This caused the issue I stated earlier, where the ray would point downward but only relative to the object's current rotation. In other words, if the ball was upside-down, the ray would be pointing upwards.

My current code for this look similar to this, which now uses world space to determine what is 'down':

 bool grounded = false;
 Ray ray = new Ray(transform.position, -Vector3.up);
 RaycastHit hit;
 
 if(Physics.Raycast(ray, out hit)){
 //determine if object is on ground
 grounded = true;
 }

However, in this instance, the ball raycast actually points directly to position Vector3(0,-1,0), which is even weirder than my initial problem and shouldn't be an issue based on other posts about this issue.

The only other way I can think of to properly determine whether the object is grounded is to see if it collides with anything. However, the other reasons I want to determine whether the player is grounded using raycast are:

  1. for determining whether the player is over an endless pit (e.g. if they fell off the world) in order to start a kill/respawn timer

  2. to ensure the player can perform a move equivalent to a butt stomp while in the air

If anyone knows how to perform a raycast on a spherical object in Unity, it's be much appreciated.

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 Bonfi_96 · Mar 25, 2016 at 10:57 AM 0
Share

Are you sure it points to position (0, -1, 0 ) ? The ray constructor uses as arguments the origin and the direction, so that should be fine. Try with Debug.DrawRay

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Rxanadu · Mar 25, 2016 at 12:57 PM

So, about that second piece of code with the -Vector3.up reference. I was using a Debug method call to check for the ray's direction. If you're experienced in Unity, you know where this is going.

Yes, I used Debug.DrawLine() instead of Debug.DrawRay(), which caused the editor to draw a line from my player to the position (0,-1,0). I suspected this when I was about to go to sleep for the day but only tested it this morning. So, there's one issue out of the way.

I also checked to see if my ray was not unintentionally detecting my player's collider, and it seems to be properly ignoring it.

Long story short, I had already fixed my problem but had used the incorrect methods for determining it was correct. So, for those who are having issues with seeing your raycast in the editor, use Debug.DrawRay().

Comment
Add comment · 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

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

39 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

Related Questions

how can i instantiate a GameObject in mouse position and in a special distance from the camera? 2 Answers

Help with Raycast C# 0 Answers

Obstacle avoidance 1 Answer

C# RayCast Fire Cooldown DeltaTime Help 1 Answer

Ultimate FPS Camera, Raycasting Troubles :( 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