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 lkarus · Aug 21, 2015 at 03:58 AM · c#2draycast

Issue with raycasting in 2D

I'm working on something where I can connect circles with the same color. I'm using Physics2D.Raycast to check whether or not my finger is over a circle, and if yes, start from there, and I can drag my fingers around over other circles with the same color and a line will be drawn in between the two circles.

However, there are situations where the ray cast would not work as intended. For example, I have this situation below.

alt text

I start from the top and swipe down ward. The intentional line is to be drawn between the 3 circles in the vertical direction. But what I get is the black lines drawn. I debugged it, and as I am dragging my finger down from the 2nd circle to the 3rd, I get a log saying that the right side circle has been hit with the raycast (some times it gives a circle that isnt even near where my finger was). This issue doesnt occur in the unity editor, only on my android. The code that I am using to raycast to the circle is the following.

 #if (UNITY_IPHONE && !UNITY_EDITOR) || (UNITY_ANDROID&& !UNITY_EDITOR)
     hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position), Input.GetTouch(0).position);
 #endif

and I am using a 2D circle colliders on the dot so that it barely covers the boundary of the circle. Does anyone see anything wrong or have a clue on what might be happening? Any help would be appreciated!

aaa.png (5.8 kB)
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

1 Reply

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

Answer by HarshadK · Aug 21, 2015 at 09:21 AM

Use Camera.ScreenPointToRay to create a ray and pass its origin and direction to your Physics2D.Raycast method.

Something like:

 Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
 hit = Physics2D.Raycast(ray.origin, ray.direction);

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 lkarus · Aug 21, 2015 at 09:36 AM 0
Share

@Harshad$$anonymous$$ Thanks for the reply. I'll have to play around with it to see if is still occuring but I wanted to ask. Is my method I am using not a good way to use? I'm new to Unity so I'm always skeptical about whether or not I am using things correctly.

avatar image HarshadK · Aug 21, 2015 at 09:59 AM 1
Share

In your current method your raycast is originating at the world position of your touch and its direction is a value which is the position of touch. The direction value is actually not pointing forward from the camera which is why you are getting quirky selections.

avatar image lkarus · Aug 21, 2015 at 10:09 AM 0
Share

After playing around for a while, it seems to be working. Thank you very much!

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

2 People are following this question.

avatar image avatar image

Related Questions

Upon shooting, Raycast target moves infinitely 1 Answer

How to make a simple line of sight in a 2D top down shooter. 3 Answers

Button blocking raycast 3 Answers

Getting a Raycast rope to follow its object 0 Answers

Multiple Cars not working 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