Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by HPrigg · Oct 26, 2013 at 12:27 AM · raycastphysics.raycast

Using raycast to determine if player is grounded.

So I've been trying to use Physics,Raycast without much luck at all. I've got a small empty Game object affixed right below the player, which is supposed to send a ray downwards and tell if it's colliding with anything. When I initially run the code, for a brief moment, it returns "is grounded", then switches to "is not grounded" and never switches back, whether I jump or not. I'm especially confused because I feel like I'm doing everything right, and when I switch the direction of the ray to up (the direction of the player), it comes back as true. It's only when sending it downwards to the BoxCollider I'm using as ground does it not see it.

Here's my code

 void Update () 
         {
     
         if (Physics.Raycast(transform.position, transform.TransformDirection (new Vector3(0,-1,0)), .1f, mask))
         {
             Debug.Log ("Is grounded");
         }
         else
         {
             Debug.Log ("Is not grounded");
         }

And as far as the direction, I've tried manually inputting the Vector3, I've tried using -Vector3.up, and I've tried assigning Vector3.up to a variable, and using the negative of that variable. None of it works.

Comment
Add comment · Show 4
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 robertbu · Oct 26, 2013 at 01:11 AM 0
Share

I see three potential issues here, but not are obviously the problem you are having. First you are using transform.TransformDirection(). This is taking the local down of your game object and getting a world direction. So if your object is not perfectly aligned with the world 'Y' or if your model is not constructed with 'Y' as up, the Raycast() will fail. Typically I see this code with Vector3.up (no Transform.TransformDirection()) so that the cast is the world down. Sometimes it uses the character down for walking on terrain, but I don't think that is what you are doing.

The second potential issue is the 'mask'. I don't have the value you use, but not understanding the 'mask' parameter is common problems in questions posted to the list.

Lastly, this code depends on your placement of the empty game object. If you are just a bit high, you would get the behavior you describe. I would suggest using the character pivot and just having the distance be slightly more than the distance of the pivot from the bottom of the character. Since mesh is one-sided, you don't have to worry about the cast hitting the character.

avatar image HPrigg · Oct 26, 2013 at 06:17 AM 0
Share

Yeah, transform.Transform direction is just the newest in numerous permutations I've tried. I just switched it to Vector3.down to no avail, and the same with -Vector3.up

The mask was just a Layer$$anonymous$$ask I named, so I could mess with it in the editor. A holdover from when I was trying to cast from within the sphere. Even when the Layer$$anonymous$$ask is changed, or removed entirely, I get the same effect.

The placement of the object doesn't seem to be helping either. I've actually tried placing the game object parallel on the X axis, just to see if that had an effect, with nothing possibly obstructing the ray other than the BoxCollider, and again, no luck.

I'm utterly stumped.

Edit:

Okay, here's a new development:

I again tried placing the gameobject parallel on the X axis, -5 units away, and made the Ray 5 units long, projected down, because I'm just throwing stuff at the wall at this point.

Is Grounded is co$$anonymous$$g out true now. He's the kicker: If I move far enough that the gameObject is above empty space, isGrounded becomes false. When I move back over solid ground, it remains false.

Why would this not be updating? Am I misunderstanding how Physics.Raycast works? Is it not a continual effect? I assumed since it was in Update (Or fixedUpdate, I've tried both) it would be called once per frame, but it doesn't seem to be updating.

avatar image robertbu · Oct 27, 2013 at 05:24 AM 0
Share

Yes you can raycast out of a sphere. I'm not sure what is going on. Sometimes when I see issues like yours, the colliders are messed up...not aligned with the object or missing. I would recommend starting with something very simple. Start a new scene. $$anonymous$$ake the ground a really big box. $$anonymous$$ake your character a box, then create the simplest raycast you can (no mask). Use Vector3.down for the direction. Then test your code. This way you can fix any script issue so the issues when you use your current scene will be collider/game object issues.

avatar image HPrigg · Oct 27, 2013 at 05:52 AM 0
Share

Oh my god.

So it seems something was messed up with one of the colliders, either the player avatar or the ground. Cant say which, deleted both and remade them. Everything works now. I'm really glad because I was starting to think I was stupid. $$anonymous$$y code was working fine, that's good to know.

A million billion thanks.

1 Reply

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

Answer by Crystalline · Oct 26, 2013 at 06:55 AM

Attach it to your player colider .

var distToGround: float;

 function Start () {
      distToGround = collider.bounds.extents.y;
 }
  
 function IsGrounded(): boolean  
 {
   return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1);
 }
  
 function FixedUpdate () {
  
 if (IsGrounded())
     {
      }
Comment
Add comment · Show 2 · 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 HPrigg · Oct 27, 2013 at 04:53 AM 0
Share

So I've fiddled with those for a day or so now and no luck. Is it because my character is a sphere? As I understand it, I can't raycast out of a sphere.

avatar image Crystalline · Oct 27, 2013 at 06:39 AM 0
Share

Then create a "dummy" character controller physis type , only to use it for raycast.

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

16 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

Related Questions

Raycast hit in OnDrawGizmos but not in Update 1 Answer

Why won't Physics.Raycast return false after it's first return of true? 1 Answer

Trying to figure out if one object can see another 1 Answer

Raycast help: casting 3 rays, out of the faces of a cube 2 Answers

Rays do not detect terrain and prefabs do not spawn where wanted 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