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
0
Question by $$anonymous$$ · May 24, 2012 at 06:21 PM · raycastcollider

raycast doesn't collide on anything? (C#)

I've been reading around to try solving this problem, basicly what I'm trying to do is to raycast from maincamera to an object with a certain tag, and return an info (for now I just use print for debugging), but it doesn't get back anything, the script compile fine without errors of any sort, so I don't know where to look. Here the code:

 using UnityEngine;
 
 public class raycast_item : MonoBehaviour {
 
 public float RayLength  = 1.0f;
 
 void Update() {
         RaycastHit hit;
 
          
         if(Physics.Raycast(transform.position, Vector3.forward, out hit, RayLength))
         {
         if (hit.collider.gameObject.tag == "Item") 
         {
             print ("Item");
         }
         else
         {
             print ("Nothing");
         }
     }
 }
 
 }
Comment
Add comment · Show 2
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 Reavenk · May 25, 2012 at 05:09 AM 1
Share

The raylength seems pretty short, you may want to try $$anonymous$$ath.Infinity and see if that helps. Also, did you want gameObject.Transform.forward ins$$anonymous$$d of Vector3.forward? So that you're looking down the forward of the game object, ins$$anonymous$$d of Vector3( 0.0f, 0.0f, 1.0f ) down world space?

avatar image $$anonymous$$ · May 25, 2012 at 10:07 AM 0
Share

I'm an idiot, yeah I meant to use transform ins$$anonymous$$d of Vector3, but still making the ray length longer it doesn't work always, is kind of glitchy, but it works now so it only need a refinement. Thanks guys.

2 Replies

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

Answer by torrente · May 24, 2012 at 06:52 PM

fromCamToTarget = targetTransform.transform.position - camTransform.transform.position;

     if (Input.GetButton("Fire1"))
     {
         if (Physics.Raycast(camTransform.transform.position, 
             fromCamToTarget,
             out bulletHit,bulletDistance))
             
             {    
                 if (bulletHit.collider.gameObject.tag == "target")
                 {
                     Debug.DrawRay(camTransform.transform.position,
                     (targetTransform.transform.position - camTransform.transform.position), 
                     Color.green);
                     Debug.Log(bulletHit.collider.gameObject.tag);
                 }
                 
                 else
                 {
                     
                     Debug.Log(bulletHit.collider.gameObject);
                     Debug.DrawRay(camTransform.transform.position,
                     fromCamToTarget, 
                     Color.red);
                 }
                 
             }
                 
         
         
     }
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 Eric5h5 · May 24, 2012 at 07:12 PM 1
Share

If you want to raycast from one point to another, then it's better/easier to use Linecast. (And you can debug that with DrawLine rather than DrawRay.) Raycast is for when you have a point and want to see what's in a particular direction from there. Which I think is what the question is asking, but I'm not 100% sure.

avatar image
3

Answer by Eric5h5 · May 24, 2012 at 06:32 PM

Use Debug.DrawRay to visualize what you're doing. In this case it's likely you meant transform.forward and not Vector3.forward.

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 torrente · May 24, 2012 at 06:48 PM 0
Share

This the way to go. I've had some crazy raycasts in the past (my mistakes) and being able to visualize them in the editor usually solved the problem. I'm going to add an answer with a quick bit of code as well.

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

How to use a Raycast to see the distance traveled within a collider. 2 Answers

Move player to specific location and avoid obstacle. 2 Answers

Im trying to create a word game with a twist 0 Answers

Raycast not detecting ANY HITS AT ALL when starting inside a collider. 0 Answers

Raycast Destroy(hit.collider.gameObject); (Still need help) 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