• 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 assuming 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
5

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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 to assign a Variable with a Raycast 1 Answer

Colliding Objects 1 Answer

Kill one enemy they all dies? 3 Answers

How can I differentiate between colliding with an object or its child? 1 Answer

Raycast tag/ Null reference exception? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges