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 /
avatar image
0
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 this feature I'd like to add into my project. It's not something 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 which ever hits 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 nothing was hit 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 this problem. And thank you again for reading my post, I really appreciate it.

example.jpg (244.0 kB)
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 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
1

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

I think you can try to do it this 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 hit;
 if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask))
    {
    obj = hit.transform.gameObject;
    break;
    }
 }
 
 
 if (objectHit) Debug.Log("Object hit");
 else Debug.Log("No object found");

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 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.

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

Raycast bullet collision problem 1 Answer

raycast screenpointtoray isnt working for some reason 0 Answers

Rigidbody.position causes shaking 1 Answer

Raycast Hit Problem. Wrong Hit Position.,RaycastHit returns wrong hit coordinates 0 Answers

Raycast hitting objects to the left of my player 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