Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by CassMeck · Apr 15, 2017 at 04:34 PM · transformvector3raycastingtaghit

How to find nearby objects?

Hi, I am trying to control the four corners of the mouse with raycasting. At the distance I specify, if there is a tag, write "found! :)" on the console.

 if (Physics.Raycast (hit.transform.position, Vector3.left, out hit, 1.0f)
     || Physics.Raycast (hit.transform.position, Vector3.right, out hit, 1.0f)
     || Physics.Raycast (hit.transform.position, Vector3.forward, out hit, 1.0f)
     || Physics.Raycast (hit.transform.position, Vector3.back, out hit, 1.0f)) {
     if (hit.collider.transform.tag == "redbox") {
     print ("found! :)");
     }
 }

But it does not work and it always gives the same error.

NullReferenceException: Object reference not set to an instance of an object

I want to check around the mouse.I am trying to do this using raycast.I do not know if there is any other way of doing this, It did not work in the alternative ways I thought.Below is the mouse that appears as a result of right clicking.

alt text

Is it possible to find out which tag is in the ending points of the gray colored lines?

draw.png (23.7 kB)
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
0

Answer by hexagonius · Apr 15, 2017 at 05:00 PM

you're using hit.transform.position as the raycast start where hit is your out parameter for the result and it's transformis of course null because it has not been used and filled at that time. Use the mouse position instead.

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 CassMeck · Apr 15, 2017 at 05:09 PM 0
Share

I tried it before but it did not work.

avatar image
0

Answer by eatsleepindie · Apr 15, 2017 at 05:03 PM

You are trying to raycast out from the position of 'hit', which does not exist yet. You want to raycast out from the transform itself. Physics.Raycast (in the way you are using it) does not take in the hit parameter, only a variable to store the information of the hit if there is one, hence 'out hit'. Try this:

  if (Physics.Raycast (transform.position, Vector3.left, out hit, 1.0f)
      || Physics.Raycast (transform.position, Vector3.right, out hit, 1.0f)
      || Physics.Raycast (transform.position, Vector3.forward, out hit, 1.0f)
      || Physics.Raycast (transform.position, Vector3.back, out hit, 1.0f)) {
      if (hit.collider.transform.tag == "redbox") {
      print ("found! :)");
      }
  }

Also of note, you should use CompareTag rather than .tag == "redbox".

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 CassMeck · Apr 15, 2017 at 05:11 PM 0
Share

I have tried it before. There is no error message but I can not find the tag either.

avatar image eatsleepindie · Apr 15, 2017 at 06:03 PM 0
Share

I can't say with any certainty why it's not working, but my answer was the fix for your errors.

Have you tried drawing your rays in the inspector via DrawRay? Use this to make sure your rays are passing through a collider. Note that these will only show up in the Scene window, not the Game window, and you will want to multiply the direction of the drawn ray by the distance. I'd suggest:

 Debug.DrawRay(transform.position, Vector3.left * 100f, Color.red);

If it looks as though your rays are sitting on the ground, then change the start point of your rays to something like this:

 Physics.Raycast (transform.position + Vector3.up * 0.5f, Vector3.left,

This will move your ray up from the ground to ensure they are passing through colliders, but you may need to adjust the 0.5 based on the size of the objects you are raycasting against.

avatar image CassMeck eatsleepindie · Apr 15, 2017 at 06:50 PM 0
Share

yes I'm using.All of the codes I use;

     private Ray mouseRay;
     private RaycastHit hit;
     private Vector3 endPoint;
     mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
     
     if (Input.GetButton ("Fire2")) {
     if (Physics.Raycast(mouseRay.origin, mouseRay.direction, out hit, $$anonymous$$athf.Infinity))
     {
     if (Physics.Raycast (transform.position, Vector3.left, out hit, 1.0f)
     || Physics.Raycast (transform.position, Vector3.right, out hit, 1.0f)
     || Physics.Raycast (transform.position, Vector3.forward, out hit, 1.0f)
     || Physics.Raycast (transform.position, Vector3.back, out hit, 1.0f)) {
      if (hit.collider.transform.tag == "redbox") {
     print ("found! :)");
     }
     }
     Debug.DrawRay (hit.transform.position, transform.right * r1, Color.gray);
     Debug.DrawRay (hit.transform.position, -transform.right * r1, Color.gray);
     Debug.DrawRay (hit.transform.position, transform.forward * r1, Color.gray);
     Debug.DrawRay (hit.transform.position, -transform.forward * r1, Color.gray);
     
     Debug.DrawRay (mouseRay.origin, mouseRay.direction * 50, Color.green);
     endPoint = hit.point;
     } else {
     endPoint = mouseRay.origin + mouseRay.direction.normalized*distance;
     Debug.DrawRay (mouseRay.origin, mouseRay.direction * 50, Color.black);
     }
     }

It looks like this. alt text

draw.png (23.7 kB)

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

119 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Find Script and call function FROM hit.transform 0 Answers

Operator '==' is ambiguous on operands of type 'Vector2' and 'Vector3' 1 Answer

InverseTransformPoint() help 0 Answers

How To: Calculate Line Of Best Travel For Grid From Line 2 Answers

transform.localScale in Coroutine grows exponentially 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