• 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
Question by ZhavShaw · Aug 03, 2017 at 06:38 PM · cameraraycastraycastingraycasthit

Raycast and ScreenPointToRay

Before I ask the question, I would like to thank you for taking the time out to look over my query.

Ok, so I'm currently being challenged with t$$anonymous$$s feature I'd like to add into my project. It's not somet$$anonymous$$ng I've done before, and I've exhausted all of the ideas I had. What I'd like to do is to cast a bunch of raycast from an point near the center of the screen until the end of the screen, and w$$anonymous$$ch ever $$anonymous$$ts first, I'll stop the loop there.
I've created an image to better display what I want. I will also run try to run through it in more detail.

alt text

Ok, so the green lines will be our center of the screen, and the buildings are there for raycasting examples.
I'd like to cast the first ray from the big red dot on the green line, and if that doesnt it, we cast a ray on the second red dot. So it would be casting rays from Left to Right. And it will continue to do so until the last red dot (if not$$anonymous$$ng was $$anonymous$$t prior).

I've tried doing a loop of raycasts and subtracting the ScreenPointToRay by (i * number) to create those dots, but I didn't get too far, as it quickly got too complex as I expected it would.

Regardless, I'm hoping one of you can help me out with t$$anonymous$$s problem. And thank you again for reading my post, I really appreciate it.

example.jpg (244.0 kB)
Comment

People who like this

0 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 Timo326 · Aug 03, 2017 at 09:23 PM 1
Share

Is your problem the raycast itself or how to calculate the screen coordinates of the red dots?

avatar image ZhavShaw Timo326 · Aug 03, 2017 at 09:26 PM 0
Share

Calculating the screen coordinates of the red dots, and looping through them until one hits (although the second part should be straight forward if I do a bit more thinking)

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by Timo326 · Aug 03, 2017 at 09:36 PM

I t$$anonymous$$nk you can try to do it t$$anonymous$$s way:

 bool objectHit = false;
 GameObject obj;
 
 for (int i = 0; i < Screen.width / 2; i+=20)   // 20 pixels between each dot
 {
 Vector3 coord = new Vector2(i, Screen.height/2, 0);
 Ray ray = Camera.main.ScreenPointToRay(coord);
 RaycastHit $$anonymous$$t;
 if (Physics.Raycast(ray, out $$anonymous$$t, Mathf.Infinity, layerMask))
    {
    obj = $$anonymous$$t.transform.gameObject;
    break;
    }
 }
 
 
 if (objectHit) Debug.Log("Object $$anonymous$$t");
 else Debug.Log("No object found");

Comment
ZhavShaw

People who like this

1 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 ZhavShaw · Aug 03, 2017 at 09:54 PM 0
Share

Okay, this is definitely getting me on the right track! Thank you very much for the response. I'm almost close to getting exactly what I was looking for :)

avatar image ZhavShaw ZhavShaw · Aug 03, 2017 at 10:22 PM 0
Share

Aright, it didn't work as expected, and it still has me stuck.

avatar image Timo326 ZhavShaw · Aug 03, 2017 at 10:26 PM 0
Share

What did not work? Did you get an error?

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

Ray from camera that has an angle and calculating right position 1 Answer

How to use both NGUI widgets and unity UI 0 Answers

Physics.Raycast doesn't hit anything even though Debug.DrawRay works 1 Answer

issues with raycasts 0 Answers

Raycast a line across the screen? 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