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 ESDev · Feb 28, 2014 at 02:51 PM · raycastinputmousebehaviourstrange

Raycast with strange Behaviour

hi,

i have a question about using raycasts for collision detection. first of all i will describe the application environment. u can imagine a 3d dna with two strands and some base pairs. the helix (parent of these pairs) is rotating around its local x-axis. only the pairs are providing colliders. the user should be able to use the mouse to interact with the pairs.

now the problem. like described above i use a raycast in the update method for detecting collision. most of the time, the result is correctly (collision is detected and the right pair is selected). but sometimes, although the ray is obviously right, the recognition isn't working.. also possible but rare is the fact that if the collision is detected, a wrong pair is selected.

what am i doing (similar code):

 Transform hittedPair;
 Dictionary<Transform, int> pairs;
 Ray ray;
 RaycastHit hit;
 Camera cam;
     
 void Update() {
     if(hittedPair == null && Input.GetMouseButtonDown(0))
     {
         ray = cam.ScreenPointToRay(Input.mousePosition);
         if (Physics.Raycast(ray, out hit, Mathf.Infinity))
         {
              hittedPair = hit.collider....;
              // So pairs[hittedPair] returns index (0, 1, 2..., n)
          }
     }
     else if(hittedPair != null && Input.GetMouseButton(0))
     {
         // Move HittedPair with Mouse
     }
     else if(hittedPair != null && Input.GetMouseButtonUp(0))
     {
         // Do something
         hittedPair = null;
     }
 }

in short:

  • mouse clicks that should obvious be recognized as collision are not!

  • example: i click on pair with index 1 and move.. pair with index 4 is selected and moved

i don't know what is happening there.. so maybe someone has a helpful suggestion for me.

Comment
Add comment · Show 4
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 robertbu · Feb 28, 2014 at 04:32 PM 0
Share

I see nothing wrong in this code. You need to add some Debug.Log() statements to figure out if 1) you are hitting something you don't expect, or if the Raycast() is failing. Then you need to isolate a specific example of failure and exa$$anonymous$$e what is going on.

    if (Physics.Raycast(ray, out hit, $$anonymous$$athf.Infinity))
     {
         Debug.Log(hit.collider.name+", "+hit.collider.tag);
          hittedPair = hit.collider....;
          // So pairs[hittedPair] returns index (0, 1, 2..., n)
      }
     else
     {
         Debug.Log("Raycast failed");
     }

     
avatar image ESDev · Feb 28, 2014 at 10:06 PM 0
Share

this is what i already have done. a lot of outputs. and its point two. unfortunately if i click on pair with id 1, sometimes another pair (e.g. index 7) will move. and also the output says "index 7". the debug.drawray shows the correct line, too... if i want to click on a pair and there is obviously a collision, sometimes it won't be recognized (and the debugger says "raycast fail".. but why ?

in addition: this is the almost the original code which is used in my application. so there isn't any relevant logic missing

avatar image robertbu · Feb 28, 2014 at 10:21 PM 0
Share

The only guess I have that matches your observations is a misalignment of the collider for the objects with the underlying mesh. So check that first.

if i click on pair with id 1, sometimes another pair (e.g. index 7) will move

Does that mean the Debug.Log() reports 1 and then 7 moves? If so, that points to an issue elsewhere in your code. That is, if the object you click on is 1 and reports as 1, then it is not the Raycast() that is failing.

avatar image ESDev · Feb 28, 2014 at 10:42 PM 0
Share

not exactly. if i click on 1, it is possible that maybe index 7 is choosen. the mouse is on pair 1, but debugger says seven. and if i move then, pair 7 will move parallel to my mouse. for this problem i thought, maybe it is because of the used collection and the equality.. but don't know. the other problem is, that if i click exactly on the pair, sometimes it will detected as "non-collision" (also debugger output). if i try another mousedown event a few ms later, then it maybe works. it is unpredictable, but 80-85 percent of events is working fine.

addition: i found out, that the missing collision detection is caused by the rotation of the parent (helix). so moved the animation code into the FixedUpdate() method. still not working. any suggestions ?

0 Replies

· Add your reply
  • Sort: 

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

20 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

Related Questions

Move player with mouse help 0 Answers

Using RayCast to Get Mouse Input 1 Answer

Raycast based on a Rect? 1 Answer

Is it possible to distinguish between mouse and trackpad scroll? 3 Answers

Does anyone know how to determine whether or not a mouse is connected? 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