- Home /
Drawing a ray as a Cylinder between two moving objects
So I have two objects in the screen and I want to draw a ray casting from object A towards object A and visualize the ray as a cylinder. This sounds very straightforward, however there are two catches:
These two objects can be dragged by the user (both of them at the same time with multi touch).
The ray can be interrupted by a collider.
I have the multi touch and dragging of objects working well, but the drawing of the cylinder is facing some problems.
My number one question is, how would you guys go about doing this?
Here is what I have done:
In the UI code, where I drag the objects, after I have modified the position of the two objects based on user input I make object B lookAt object A.
After this I make object A lookAt object B.
I cast the laser towards object B from object A.
I draw a cylinder from object A towards object B with a length of hit distance.
The problem is, that when I move object B fastly the ray does not hit object it. Why is this happening? I make object A lookAt B so it should not happen.
When the ray does not hit anything, I set the length to a big value, and the drawn Cylinder goes through object B, meaning the ray should hit it.
Update: I tried putting the whole thing in a FixedUpdate call. The results are the same unfortunately. I saved screenshots, when the ray did not hit anything. It seems on these shots, that it should hit object B though. On the below screens object A is the big Cylinder and object B is a Sphere cut in half.
If you ran out of ideas and willing to help, I might as well just show the whole project.
Because you're talking about raycasting issues related to fast objects, my first thought is recommending you move your movement and raycast logic into FixedUpdate() to ensure your code is running in tandem with the physics simulation.
Apart from that, the question is a bit confusing - the end point of your ray / visualization of the ray doesn't ter$$anonymous$$ate at point B, but extends past it? Is it supposed to collide with a collider on object B? How about a screenshot or video of the problem?
Ah yes I was thinking there might be some inconsistency with the physics. I will try putting the code into the FixedUpdate function. If that doesn't help, I will show video footage.
B has a collider on it, and it does hit it, when B is not moving, or moving slowly.
Edit: See screenshots.
Afraid I'm not seeing any screenshots. Let us know if the fixedupdate trick doesn't work. Problems like this are generally related to the order in which operations are performed in a given moment / frame.
Taking a look at it, I didn't realize you were dealing with multiple scripts. Try changing the script execution order in the project settings such that the actual physics check script is the last in the order-of-execution.
I couldn't test it well because I have no touch device, but I did spot your error once I rigged up the objects to let me move them. I notice the whole ordeal seems unnecessarily complex, if you'll pardon me for saying so. No wonder the issue is proving hard to track down. ;)
Your answer
Follow this Question
Related Questions
Physics.Raycast doesn't hit anything even though Debug.DrawRay works 1 Answer
Multiple hit detection with Raycasting? 2 Answers
Using Raycast Normal to match surface rotation to instantiated gameobject? EXAMPLE GIF Included 0 Answers
¿Why Raycast sometimes don't collide with rigidbody (plus collider)? 0 Answers
For raycast hits, what is a "barycentric coordinate"? 1 Answer