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 Ochreous · May 15, 2013 at 12:42 AM · c#screencenter

C# Physics.SphereCast Center of Screen

Hi everyone, how do you get a SphereCast in the exact center of the screen? I tried the following below but I'm not sure if it's correct.

 if(Physics.SphereCast(new Vector3(Screen.width / 2, Screen.height / 2, 0), 2f, gameObject.transform.forward, out hit, 100))
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
2
Best Answer

Answer by robertbu · May 15, 2013 at 01:28 AM

  • Unity has four coordinate systems, Viewport, Screen, World and GUI. You are passing a Screen coordinate to a function that takes a World coordinate.

  • SphereCast() takes a sphere of a radius you specify and moves it in the direction you specify for the length you specify. It returns the first object that would have produced a collision with the sphere. If you are trying to detect object at the center of the screen, then you want to use Physics.OverlapSphere() or Physics.CheckSphere().

  • The screen is a 2D space and the world (where SphereCast() works) is a 3D space. So any time you make a conversion between 2D and 3D you have to ask at what distance from the camera.

To make the conversion:

 v3Pos = Camera.main.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 10.0f));

Viewport points start at (0,0) in the lower left and go to (1,1) in the upper right, so (0.5,0.5) will be the center of the screen. The 'Z' parameter will be the distance in front of the camera.

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

Answer by Dreamora · May 15, 2013 at 01:30 AM

what you want to do is use the Camera.main.ScreenPointToRay function with the coordinate you are using the resulting ray

Comment
Add comment · Show 7 · 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 Ochreous · May 15, 2013 at 01:51 AM 0
Share

I switch to Camera.main.ScreenPointToRay but I'm getting an error from the console saying that I have some invalid arguments.

 if(Physics.SphereCast(Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0)), 2f, gameObject.transform.forward, out hit, 100f))
avatar image Lairinus · May 15, 2013 at 02:04 AM 0
Share

What you should do is send a ray from the center of the screen.

Then when the ray hits something, you use the hit.point to get the exact point of impact.

You then create the overlap sphere at the hit.point.

avatar image Ochreous · May 15, 2013 at 02:07 AM 0
Share

@Shingox what exactly are you trying to do? I don't quite understand. $$anonymous$$y spherecast is hitting objects. It's just not in the center of screen.I'm trying to figure out how to get my Spherecast in the center of the screen. I have no idea if my SphereCast is in the center or not.

avatar image Lairinus · May 15, 2013 at 02:12 AM 0
Share

I'll write you some pseudo-code. Give me a little.

The method i'm going to provide will make it so that you can use the sphere at the center of the screen as long as an initial ray collides with an object.

IF you don't do it this way I think you'll need to do a lot more work with vectors.

**Just paid attention for one of the first times in my life and saw that you meant SphereCast and not overlap.

Anyway, this should fix your issue :

     void CastingSsomeSpheres()
     {
         float distance = 0;
         float radius = 0;
         Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
         RaycastHit hit;
         if (Physics.SphereCast(ray, radius, out hit, distance))
         {
             if (hit.collider)
             {
                 
             }
         }
     }
avatar image Ochreous · May 15, 2013 at 02:40 AM 0
Share

Now it seems to be hitting anything in the center of my gameobject rather than the center of my screen.

 if(Physics.SphereCast(Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0)), 2f, out hit, 100f))
Show more comments

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

15 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

Related Questions

Unity 3d C# GUI box goes off to the side needs to be in the center of the screen 0 Answers

Saving final score and displaying on main menu 1 Answer

null texture passed to GUI.DrawTexture 0 Answers

How to Name Individual Buttons Within a List 2 Answers

c# Adjust In-Game audio 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