Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 coolbird22 · Sep 10, 2014 at 10:33 AM · raycastmovetagcheck

Why is this Raycast not working ?

I've checked various examples and tried to replicate them but for some reason the raycast doesn't seem to work. And yes, I do have objects tagged as 'wall'.

     RaycastHit hit;
     bool wRay;
 
 void Update () {
 
         if (Input.GetKey (KeyCode.W)) {
             Physics.Raycast(transform.position, Vector3.left, out hit, 0.01f);
             wRay = Physics.Raycast(transform.position, Vector3.left, out hit, 0.01f);
             Debug.DrawRay(wRay, Color.red, 0.01f);
             if (wRay)
             {
                 if(hit.transform.tag == "wall")
                 {
                     gameObject.transform.rotation = Quaternion.Euler(0,270,0);
                 }
             }
         }
 }
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 sevensixtytwo · Sep 10, 2014 at 10:43 AM 0
Share

How does it not work? Does it not detect the tagged object? Does it cast in the wrong direction? Does it not cast at all?

3 Replies

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

Answer by NickP_2 · Sep 10, 2014 at 10:46 AM

the third parameter (0.01f) is the length of the ray, note that 0.01f really isn't a lot! (It might not even leave the NPC/Player's model, if there's one.) Have you tried deleting that parameter and just use an infinite ray?

Just for performance's sake, store the ray in a variable. Something like:

 Ray ray;
 RaycastHit hit;
 
 ray.origin = transform.position;
 ray.direction = Vector3.left;
 if(Physics.Raycast(ray, out hit) && hit.transform.tag == "wall")
 {
     //remaining code...
 }
 Debug.DrawRay(ray, 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 coolbird22 · Sep 10, 2014 at 11:51 AM 0
Share

The length of the ray was apparently too small. I was assu$$anonymous$$g the length to ins$$anonymous$$d be the duration of the ray for which it will be displayed, as in Debug.DrawRay. Thanks !

avatar image
8

Answer by jsleek · Oct 16, 2017 at 10:47 AM

For anyone else reading this and their raycast still isn't working,

Make sure you have a mesh collider on the surface you are aiming at. The raycast needs to collide with something.

That's what got it working for me.

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
1

Answer by khenkel · Sep 10, 2014 at 10:47 AM

There can be a few reasons why your Raycast "doesn't seem to work". I don't know what exactly doesn't work, so I'll provide some ideas:

  • First of all, Physics.Raycast returns a bool, so you might want to poll it directly (" if(Physics.Raycast(transform.position, Vector3.left, out hit, 0.01f)") instead of doing it by saving it temporarily. The "hit" variable will then contain everything you need about your Raycast.

  • The distance seems to be very short. Keep in mind that the raycast is sent from transform.position, which means the origin of your object. For testing try out "Mathf.Infinity" instead.

  • You're sending it in the Vector3.left direction, which is absolute, it will always be the same direction. If you want it to be relative to your object, use "this.transform.left" instead. (correct me if I'm wrong here)

EDIT:

Whoops, "this.transform.left" doesn't exist. Then use "-(this.transform.right)". :)

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

27 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

Related Questions

How do you change the position of a Raycast? 1 Answer

Unity RaycastHit.transform.tag working in editor not working on Android 1 Answer

Tic Tac Toe--how to check for win 2 Answers

Raycast object tag check? 3 Answers

Use OnTriggerEnter during a transform ! 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