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 mchalo · Jun 01, 2011 at 03:30 AM · raycastray

Help with ray cast

Hi guys i was wondering if someone could help me. well basically i am making an ray shoot out of the cam but it does not work. i said if the ray hits the gameobject with the tag "Floor" print you hit floor. But it is not working :(

here is my script:

 function Update () {
 var hit : RaycastHit;
 var camray : Ray = Camera.main.ScreenPointToRay(Vector3( Screen.width / 2 , Screen.height / 2, 0));
 //Debug.DrawRay(transform.position, Vector3.forward * 50, Color.red);
 
 if(Physics.Raycast(camray, hit, 50)){
 
 
    if(hit.collider.gameObject.tag == "Floor"){
    
    Debug.Log("you hit the floor ");  
 
 }
 }
 }
 

also how do i get the ray to be viewable in the scene mode normally i did :

Debug.DrawRay (transform.position, Vector3.forward * 10 , Color.red);

this does not work when i try and do it for the camera how do i do that ?

i would be really thank full if someone could help me :)

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

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by hellcats · Jun 01, 2011 at 03:39 AM

Well, is it possible that your floor is more than 50 units away, or in front of the near plane of the camera? The ray created by ScreenPointToRay starts at the near plane. And you limited the Raycast to 50 units. The other possibility is that the floor has no collider.

To draw the ray, you need to use

Debug.DrawRay(ray.origin, ray.direction * 50, Color,red);

You had used transform.position and Vector3.forward which have nothing to do with the ray.

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 mchalo · Jun 01, 2011 at 03:50 AM 0
Share

Nope that still does not work i have tried that way for showing the ray as well but it does not work. I can not use ray.forward it does not allow it.

avatar image hellcats · Jun 01, 2011 at 04:04 AM 0
Share

oops. I should have said ray.direction ins$$anonymous$$d of ray.forard :)

avatar image mchalo · Jun 01, 2011 at 04:07 AM 0
Share

that still does not show any line :( its just showing the same old cam with no line co$$anonymous$$g out of it :(

avatar image hellcats · Jun 01, 2011 at 05:22 AM 0
Share

At this point I would recommend printing out the ray origin and direction with Debug.Log to verify that the ray has a non-zero direction. If it is zero then there must be something wrong with your camera setup.

avatar image
0

Answer by Dreamer · Jun 01, 2011 at 04:54 AM

Can you try below:

 var ray = new Ray (Camera.main.transform.position, Camera.main.transform.forward);
 Debug.DrawRay (Camera.main.transform.position, Camera.main.transform.forward* 10, Color.green);
 
 var hit : RaycastHit;
 if (Physics.Raycast (ray)) {
   if(hit.collider.gameObject.tag == "Floor"){
 
    Debug.Log("you hit the floor ");  
    }
 }
Comment
Add comment · Show 1 · 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 mchalo · Jun 01, 2011 at 05:06 AM 0
Share

No that still does not work lol i have tried and still same old results :)

avatar image
0

Answer by aldonaletto · Jun 01, 2011 at 05:45 AM

There are 2 reasons for the ray to be not visible:
1- Debug.DrawRay only draws in the Scene view; you can enable it in the Game view too by clicking the Gizmos button (on the top of the Game view);
2- Since the ray goes from the center of the camera to the center of the screen, the only thing you will see (if any) is a red dot; try to offset the origem 0.5 downwards

 function Update (){
 
   var hit : RaycastHit;
   var camray : Ray = Camera.main.ViewportPointToRay (Vector3(0.5,0.5,0));
 //  Debug.DrawRay(transform.position, Vector3.forward * 50, Color.red);
   if(Physics.Raycast(camray, hit, 50)){
     Debug.Log("you hit "+hit.collider.gameObject.name);  
   }
 }
Comment
Add comment · Show 3 · 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 mchalo · Jun 01, 2011 at 06:01 AM 0
Share

Nope it still does not draw a line :(

avatar image hellcats · Jun 01, 2011 at 04:21 PM 0
Share

Can you check what the value of 2+2 is on your computer? Seriously, maybe you just need to post your project because all these suggestions should be working.

avatar image mchalo · Jun 02, 2011 at 05:07 AM 0
Share

i would be glad to post my project up :) i will do it very soon then you can have a look :)

avatar image
0

Answer by JustJonny · Oct 25, 2012 at 11:04 PM

 //20 is a distance
 var ray = new Ray (Camera.main.transform.position, Camera.main.transform.forward*20);
     var hit : RaycastHit;
     Debug.DrawRay (Camera.main.transform.position, Camera.main.transform.forward* 20, Color.green);
     if (Physics.Raycast (ray, hit, 20)) {
         Debug.Log(hit.collider);
     }
     else{Debug.Log("EMPTYNESS");}
 
 If this aint gonna help you, than speak to the Dark Vaider of Matrix :D
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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiate objects along a ray in four directions 0 Answers

Raycast returns null for no apparent reason 0 Answers

Raycasting on render texture, minimap 1 Answer

Crosshair and Ray 1 Answer

altitude checking from ground 2 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