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 Kyieon · Jul 31, 2013 at 01:20 AM · physicsraycastgetcomponentbooleans

Why isn't this raycast working?

This raycast isn't changing the value I want it to. Any idea why?

 if(Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, range)){
             
     if (hit.transform.tag == "Blue Cube"){

         hit.collider.gameObject.transform.GetComponent<blueCube>().grabbed = true;

     }

 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by aldonaletto · Jul 31, 2013 at 01:31 AM

This should work - IF a "Blue Cube" object is in the center of the screen and inside the range and it has a collider, of course. Add debug lines like below to help understanding what's going on:

 if(Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, range)){
     print("Hit "+hit.transform.name);
     if (hit.transform.tag == "Blue Cube"){
        hit.transform.GetComponent<blueCube>().grabbed = true;
     }
 }

Notice that any field of hit that references the hit object may be used in GetComponent.

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 Kyieon · Jul 31, 2013 at 12:21 PM 0
Share

Thank you. I had forgot to generate collision for the mesh. I had a collider on it but forgot to generate colliders in the import settings.

avatar image
0

Answer by RPGstandard · Jul 31, 2013 at 01:27 AM

have you given your game object a rigidbody? If not It wont detect the collision

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 robertbu · Jul 31, 2013 at 03:42 AM 2
Share

@RPGstandard - as you say, collisions require a Rigidbody on one of the two game objects. Raycast() are not collisions do not require a Rigidbody component.

avatar image RPGstandard · Jul 31, 2013 at 03:57 AM 0
Share

Ah.. I was not aware of that, Ive still got a lot to learn on here as well. Thanks for taking the time to correct me

avatar image aldonaletto · Jul 31, 2013 at 04:16 AM 0
Share

As @robertbu said, Raycast doesn't require a Rigidbody - but remember that the object must have a collider in order to be detected by Raycast.

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

17 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

Related Questions

How do I get my Fireball toward my mouseposition?(some non working tries example in here :3) 1 Answer

Physics required for collision detection? 2 Answers

Skateboard slope problem 0 Answers

Physics.Raycast (Cheapest Methods) 0 Answers

Creating a magnetic vehicle (like F-Zero) 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