• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
Question by sona.viswam · Jun 28, 2013 at 04:45 AM · gameobjectraycasttransformtouch

Continuous detection of object using ray cast

i have a moving gameobject in unity 3d. Gameobject is rigid body with colider. if i swipe that object slowly, i can detect object by using raycast using code

 if(Physics.Raycast(ray,out GetHit,1000f, layerMask)){ 
  $$anonymous$$tObject = GetHit.collider.gameObject;      
  print($$anonymous$$tObject);
 }

alt text

In image red colours indicate swipe. For swipe indaication i used line renderer. The point i gets i saved into a array and made line renderer.

If i swipe fast i cant able to detect the object. I cant able to get contious touch points. Why it happen so???

test1.png (3.0 kB)
Comment

People who like this

0 Show 7
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 Benproductions1 · Jun 28, 2013 at 05:06 AM 0
Share

Probably because within one frame, your touch is no longer on the object. I suggest "saving" the object when you press down on it instead :)

avatar image Em3rgency · Jun 28, 2013 at 05:08 AM 0
Share

I imagine if you have some sort of mouse swipe implemented, and you swipe your mouse faster than the update function can keep up with, your mouse gets "teleported" to the new location to compensate, instead of actually traveling there. So you could jump over the object entirely and never touch it.

avatar image sona.viswam · Jun 28, 2013 at 05:20 AM 0
Share

You both are right. So how can i escape from this issue. Any another ideas???

avatar image Benproductions1 · Jun 28, 2013 at 05:28 AM 1
Share

The depends on what you cant to achieve. You could calculate the path in between this and last frame, then for every, lets say, half a unit, do a raycast. That would ensure consistent raycasting over distance, rather then time :)

avatar image Benproductions1 · Jun 28, 2013 at 10:14 AM 1
Share

@markpdolby I would suggest against that. It might even decrease the accuracy depending on the current framerate the OP is getting.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by SinisterRainbow · Jun 28, 2013 at 10:17 AM

Couple of t$$anonymous$$ngs: (edited to reflect Benproductions input). If you use an OnGui function, not Update(), it will send a mousedrag event, but t$$anonymous$$s doesn't update every pixel, whether it runs faster than update() I'm now unsure. it's limited by the interrupt speed by a computer I believe. So one solution is to draw a line between each mouse drag event, then test maybe 5 ray casts at equal intervals along the line and see if you get a $$anonymous$$t (the first point will be the touch point, and don't do any ray casts if the touch points are not on either side of the object (you can test by remember the last drag point raycast, and casting a ray at the point of the mousedrag event.

I'm assuming you are drawing a 2d line over a 3d object. If it's a 2d object with 2d line performance is even better, fit a rectangle around the object and use Rect.Contains() (it uses AABB).

T$$anonymous$$s may not be applicable, but more efficient: if you instead get the first contact point, and last contact point, it's easy to tell if your line passed through the object, and you only have to detect on touch and on release rather than look several times a frame. If you combine t$$anonymous$$s with translating the 3d object's bounding box into 2d screen space, then you don't need to go about casting tons of rays at different points in the line, you can just check the few verts of the screen space box and stop as soon as you see one vert above the line and one below it. You can still draw your line normally, but don't test collision until you're done touc$$anonymous$$ng...Not sure if that will work with your game dynamics.

Comment

People who like this

0 Show 1 · 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 Benproductions1 · Jun 28, 2013 at 10:23 AM 2
Share

OnGUI is called per input action, but, that does not fix any problem. If you move your finger, it will only be called once, not depending on the distance traveled.
The 2d thing works great... if it's only in 2d...
If you're doing everything in 3d space, raycasting is unavoidable :)

avatar image

Answer by fafase · Jun 28, 2013 at 11:35 AM

Move you object with basic input, you may have to create you own drag method and then store the object position and draw a linecast between current and previous. If the line is cut it will return false.

T$$anonymous$$s is a basic drag mouse method:

 public static bool DragMouse(int button){        
     if(Input.GetMouseButton (button)){
         bool x = Mathf.Approximately(previousPosition.x, Input.mousePosition.x);
         bool y = Mathf.Approximately(previousPosition.y,Input.mousePosition.y);
         if(!x || !y){
             previousPosition = Input.mousePosition;
             return true;
         }
     }
     previousPosition = Input.mousePosition;
     return false;
 }

And then you can do:

 Vector3 previous;
 if(DragMouse(0)){
    if(Physics.Linecast(obj.position,previous)){
       // Collision
    }
    previous = obj.position;
 }
Comment

People who like this

0 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Select a group of gameobjects that are contiguous 1 Answer

Trying to racast from object to player, but it massively overshoots 1 Answer

Change Input.GetTouch(0).deltaPosition to object transform position in Unity 4.3 for 2D 3 Answers

Trying to find objects. 1 Answer

Destroy plane behind character controller or camera 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