raycast problem on high speed

i have object that moving, and camera that child of it. I need to handle the tap on this object
Camera.mainCamera.ScreenPointToRay(touch.position);

after this i check collider tag. But if object moving to fast, raycast missing object, and return game object behind

Colliders are only updated immediately after the FixedUpdate()s have been processed, not Update(), so if you modify them in Update()/LateUpdate() (via Animation or changing the transform’s values), a Raycast will not refer to the new position.

Instead, you need to do all transformations of relevant objects with Colliders in FixedUpdate(). Then it does not matter where you do the Physics.Raycast(), it will always work.

Note that modifying the transform and then Raycasting to that new position in the same FixedUpdate() will not work either, for the same reason. The physics engine only updates exactly once between two consecutive calls to FixedUpdate().

To make sure your transformations are happening in FixedUpdate(), you’ll need to move all relevant object position changes to FixedUpdate(), select “Animate Physics” in Animations, and so on. Regular RigidBody changes (via forces etc.) will be handled in the physics loop anyway, so they should always be correct.

Note that this is not relevant for Camera transformations, since these do not have colliders (unless objects with colliders are attached to it, then you’ll have to do Camera transformations in FixedUpdate() as well.

The obvious drawback for this is, that you might get jerky motions, since frames are rendered in the Update() context, and there may be several (or no) Update() calls between two FixedUpdate() calls.

See also here: http://forum.unity3d.com/threads/57223-Buggy-Physics.Raycast-fails-for-moving-objects

Here is everything I’ve gathered after reading all night about this issue:

  1. Use FixedUpdate rather than Update (for reasons already mentioned above)
  2. On your rigidbodies change the “Collision Detection” option from “Discrete” to “Continous Dynamic.” (Used for fast moving objects.)
  3. Go to Edit > Project Settings > Time > Fixed Timestep to 0.01 …& Maximum Allowed Timestep as low as possible (i.e. 0.05). NOTE: A bizarre behavior I experienced on Maximum Allowed Timestep is if I set the option to an even number (i.e. 0.02, or 0.04), the raycast doesn’t work any more.

The issue is still not completely resolved for me and I’m not sure how bad the performance hit will be after adding more to our project, but the symptoms are dramatically less now and probably usable.

Hopefully one or more of these options will help someone else.

when i moved code to FixedUpdate() i got some lags, and frame drops, but i really don’t need FixedUpdate(), i’m just need to synchronize my raycasts with it. And it’s my current code:

private IEnumerator FadeOut() {	
	yield return new WaitForFixedUpdate();
	moveCam();
	inputControl(); //one raycast here
	checkTouch(); //another here
}

void Update () {
	moveCam(); 
	StartCoroutine(FadeOut());
}

Ofcourse it’s can be optimized, because i don’t need raycasts every frame. Better way will be i’ll call this method when i touch down, and etc. I didn’t test this code enough, but at first glance, everything looks good and smooth

UPDATED: when i run it’s from device(not through IDE), only app, i got lag again), need some optimization(