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
1
Question by Ronin6 · Jan 15, 2013 at 05:33 AM · collisioncollideroverlapscreenspaceworldtoscreenpoint

Can Camera.WorldToScreenPoint be used to check if one collider is over another in screen space?

Hello unityAnswers its me again with another annoying question. This one may be more straightforward.

Is it possible to somehow use Camera.main.WorldToScreenPoint or ScreenToWorldPoint to check if two colliders are overlaping in screen space ?(I'm not looking for if they overlap in WorldSpace, that's easy anyway)

Comment
Add comment · Show 6
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 Ronin6 · Jan 15, 2013 at 06:48 AM 0
Share

Im also open to other methods of checking Screen Space collision. Whatever works.

avatar image Ronin6 · Jan 15, 2013 at 07:00 AM 0
Share

A more simple version of my question is How do I detect collision in ScreenSpace?

avatar image whydoidoit · Jan 15, 2013 at 09:52 AM 0
Share

Well you are going to need more than points of course - you could convert the bounding points of the AABB to screen space and test a point in that. Can you describe more about what you are trying to do?

avatar image Ronin6 · Jan 15, 2013 at 11:37 AM 0
Share

http://answers.unity3d.com/questions/380648/alternative-to-onmousedown.html

Here is my past question and the same problem I am trying to work out. If I can tell if one collider is overlapping another in screen space than my problem may be solved.

avatar image Ronin6 · Jan 15, 2013 at 12:39 PM 0
Share

What is AABB? How can it work ?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by robertbu · Jan 15, 2013 at 04:24 PM

Raycast is the way to solve your original problem. Here is a test test script.

 using UnityEngine;
 using System.Collections;
 
 public class RandomObjects : MonoBehaviour {
     
     public GameObject goGenerate;  // Game object to generate
     public Camera cam;  // Camera to use for the raycast
     public GameObject goCrosshairs;
     Plane plane;
 
     void Start ()
     {
         plane = new Plane(Vector3.forward, new Vector3(0,0,cam.gameObject.transform.position.z + 0.5f));
         
         for (int i = 0; i < 35; i++)  // Generate some random objects
         {
             Vector3 v3Pos = Random.insideUnitSphere * 5.0f;
             v3Pos.x *= 2.0f;
             Instantiate (goGenerate, v3Pos, Quaternion.identity);
         }
     }
     
     void Update ()
     {
         Ray ray = cam.ScreenPointToRay(Input.mousePosition);
         float fDist = 0.0f;
         RaycastHit hit;
         
         plane.Raycast (ray, out fDist); 
         goCrosshairs.transform.position = ray.GetPoint (fDist);
         
         if (Input.GetMouseButtonDown (0))
         {
             if (Physics.Raycast(ray, out hit))
             {
                 Destroy (hit.collider.gameObject);
             }
         }
     }
 }
Comment
Add comment · Show 8 · 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 Ronin6 · Jan 15, 2013 at 08:41 PM 0
Share

Ok Im gonna try this code but I raycasted before and it didnt work for me. Will this code work if the crosshair is not near the center of the screen?

avatar image Ronin6 · Jan 15, 2013 at 09:29 PM 0
Share

Trying to figure this out. Is plane my crosshair?

avatar image robertbu · Jan 15, 2013 at 09:42 PM 0
Share

To use the above script: 1) Attach this script to an empty game object; 2) create a game object or a prefab to use as the cross hairs and in the inspector, remove the collider, and drag it on top of the goCroshairs variable; 3) create a game object or prefab to use as a basis for the random objects in the scene and drag it in the inspector on top of the goGenerate variable; 4) drag the main camera on top of the cam variable. Hit play.

Note the crosshair game object is displayed on a plane close to the camera, so it will have to of small size. The Plane is an internal construct not a game object. It is used to calculate where to put the crosshairs.

Note I wanted a working example for you to show that Raycast does work. After playing with this script a bit, you might want to go back to your original script and figure out why it did not work. $$anonymous$$aybe you had a collider on your crosshairs, or you did not create the Ray the right way, or your scene objects did not have the collider setup correctly, or...

avatar image Ronin6 · Jan 15, 2013 at 11:37 PM 0
Share

Wow it works better than ever before thanks! Is there a way to get it to work in the entire crosshair and not just the 1pixel center point?

$$anonymous$$aybe I would have to cast multiple rays to achieve this? Something like?

avatar image whydoidoit · Jan 15, 2013 at 11:39 PM 0
Share

Use SphereCast

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

11 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

Related Questions

Trigger 2D repel on overlap (2-D player combat issue) 2 Answers

Unity Colliders are Overlapping/Intersecting 2 Answers

Object Collision PLEASE HELP!!! 1 Answer

Player shaking when colliding with objects 3 Answers

Collider going through walls with box collider.. 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