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 ninjaboynaru · Jan 03, 2013 at 06:23 PM · javaray castvector 3

Ray cast downward for ground collision

I would like to cast a ray downward to see if the ray collides with the ground. Here is what I have so far.

 function Update{
 var downDirection : Vector3 = Vector3(0,-10,0);
 
 Debug.DrawRay (transform.position, Vector3.downDirection, Color.green);
 }




How would I go about actually detecting a collision and returning a boolean as true if such collision were detected while still drawing the ray.

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

3 Replies

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

Answer by shigidaMark · Jan 03, 2013 at 06:27 PM

 var hit : RaycastHit;
 var dist : float;
 var dir : Vector3;
 
 function Update(){
      dist = 10;
      dir = Vector3(0,-1,0);

      //edit: to draw ray also//
      Debug.DrawRay(transform.position,dir*dist,Color.green);
      //end edit//

      if(Physics.Raycast(transform.position,dir,hit,dist)){
           //the ray collided with something, you can interact
           // with the hit object now by using hit.collider.gameObject
      }
      else{
           //nothing was below your gameObject within 10m.
      }
 
 }
Comment
Add comment · Show 8 · 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 ninjaboynaru · Jan 03, 2013 at 06:37 PM 0
Share

Seems to work fine, but could you explain what the "hit: is in (transform.position,downDirection,hit,dist)

and where I would include a debug.drawray so it draws the exact same ray that is being casted

avatar image shigidaMark · Jan 03, 2013 at 06:39 PM 0
Share

just added the DrawRay() function...

The hit is a RaycastHit object. It contains information about the object being hit and the point at which it was hit.

http://docs.unity3d.com/Documentation/ScriptReference/RaycastHit.html

avatar image shigidaMark · Jan 03, 2013 at 06:42 PM 0
Share

you could actually just not use it at all (just remove it), the Raycast method is overloaded and calling the method with just the other arguments mentioned is enough to check if 'something' is hit. But to interpret if your character is hitting terrain or something you could do a check against the hit object's tag or name:

if(Physics.Raycast(transform.position,dir,hit,dist)){ if(hit.collider.tag == 'Ground'){ //character is within 10m of the ground } }

avatar image ninjaboynaru · Jan 03, 2013 at 09:40 PM 0
Share

Worked thanks

avatar image JuanseCoello · Apr 02, 2014 at 01:26 AM 1
Share

Can you translate this code to C#? I realle need it please.

Show more comments
avatar image
0

Answer by Janibasha · Jun 28, 2018 at 12:19 PM

not working, always working

else{ //nothing was below your gameObject within 10m. } else part only

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
avatar image
0

Answer by QuarterMillion · Aug 08, 2021 at 05:44 AM

@JuanseCoello

//for C# version of suggested code

//I'm pretty sure that this var hit : RaycastHit;

//could be written as this RaycastHit hit;

//so following that logic

  RaycastHit hit;
  float dist;
  Vector3 dir;

//Otherwise everything else should probably remain //the same

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

14 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

Related Questions

GameObjects being instantiated on top of each other 1 Answer

Zombie attack script animation help! 0 Answers

Smoothing Directionally between three materials 1 Answer

cannot create key in keystore 1 Answer

Help with Java socket server 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