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 ExpiredIndexCard · Aug 14, 2013 at 12:36 AM · colliderraykeycast

Raycasting is not working!

Here is my code and Im not sure what I'm doing wrong. This is the first time raycasting isnt working for me! Here:

     void Update () 
     {
         Ray ray = new Ray(transform.position, transform.forward);
         RaycastHit hit;
         
         if(Physics.Raycast(ray, out hit, 30f) && Input.GetMouseButtonDown(0))
         {
             if(hit.collider.gameObject.tag == "Key" == "Key")
             {
                 Debug.Log("Grabbed The Key");
             }
         }
 }


EDIT: Just to let you know, this is attached to a first person controller that comes with the Unity Standard Assets.

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 Benproductions1 · Aug 14, 2013 at 12:39 AM 1
Share

@ExpiredIndexCard What is not working!

avatar image robertbu · Aug 14, 2013 at 12:40 AM 1
Share

One problem is the use of Vector3.forward. That is the world forward. Likely you want to use transform.forward which is the direction the game object this is attached to is facing.

2 Replies

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

Answer by YoungDeveloper · Aug 14, 2013 at 12:44 AM

Hi, If you are trying to cast ray from players camera center use this.

 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 //Note that cursor must be locked, if it isn't for you, here's the code
 Screen.lockCursor = true; //must be in Update function;
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 YoungDeveloper · Aug 14, 2013 at 12:50 AM 0
Share

Is debug showing the message ?

 void Update ()
 {
 Ray ray = new Ray(transform.position, transform.forward);
 RaycastHit hit;
  
 if(Physics.Raycast(ray, out hit, 30f)) Debug.Log("Hit");
 
 }
avatar image YoungDeveloper · Aug 14, 2013 at 12:52 AM 0
Share

And by the way, It's

 hit.collider.gameObject.tag == "$$anonymous$$ey"

not

 hit.collider.tag == "$$anonymous$$ey"
avatar image YoungDeveloper · Aug 14, 2013 at 12:56 AM 0
Share
 void Update ()
 {
 Ray ray = new Ray(transform.position, transform.forward);
 RaycastHit hit;
  
 if(Physics.Raycast(ray, out hit, 30f){
 if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse0)){ // left mouse click
 if(hit.collider.gameObject.tag == "$$anonymous$$ey")
 Debug.Log("Grabbed The $$anonymous$$ey");
 }
 }
avatar image YoungDeveloper · Aug 14, 2013 at 01:01 AM 0
Share

It's because of this line

 Ray ray = new Ray(transform.position, transform.forward);

Where do you want to shoot the ray from ?

avatar image Joyrider · Aug 14, 2013 at 01:05 AM 1
Share
 Ray ray = Camera.main.ScreenPointToRay(new Vector2(Screen.width/2,Screen.height/2));
Show more comments
avatar image
0

Answer by deadfish90 · Aug 14, 2013 at 01:59 AM

What others are mentioning about the forward facing vector not changing with rotations is true if you have it attached to the "First Person Controller" game object. That game object only rotates in Y axis. What you should do is attach the script with the ray casting on the camera, whose rotation occurs in more than just one axis. Below is the code that I had working. Aside from that, I've added a gizmo drawLine to show you what happens when the script is attached to the "First Person Controller" game object versus the camera.

 void Update ()
     {
         Ray ray = new Ray(transform.position, transform.forward);
         RaycastHit hit;
          
         if(Physics.Raycast(ray, out hit, 30f) && Input.GetMouseButtonDown(0))
         {
             if(hit.collider.gameObject.tag == "Key")
             {
                 Debug.Log("Grabbed The Key");
             }
         }
     }
     
     void OnDrawGizmosSelected() {
         Gizmos.color = Color.blue;
         Gizmos.DrawLine(transform.position, transform.position + (transform.forward * 30));
         
     } 

PS, you had

== "Keys" == "Keys"

in your if statement. I'm pretty sure that was a typo, but I thought I'd mention it just in case.

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

18 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

Related Questions

Locate a game object using rays 1 Answer

Don't allow raycasts to go through colliders? 3 Answers

Click&Drag Misterious Disappearing! 1 Answer

I don't know why i can't detect by ray sth. tagged, 1 Answer

Physic Collider Test Rotation 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