- Home /
My raycast randomly stops working.
Hello everyone.
I'm working on a mobile game where you have to control a traffic intersection. You have to 'swipe' on the cars to boost them and make them cross the intersection. All cars have a touchcollider onto which I cast a ray. The ray ignores all layers but the "touchinput" layer. This all works perfectly, but after a specific event the ray doesn't detect the collider anymore. This happens at random times, only after the specific event and only for a random amount of seconds. After the amount of seconds, it stars working again. I'm using this forum as a last resort because I've tried everything. I've logged and checked everything. The ray returns false and I can't figure out why.
/// <summary>
/// This method will execute the beginclick or touch logic
/// depending on what the player did
/// </summary>
private void ExecuteBeginClickOrTouchLogic()
{
// Start the swipe timer
_currentSwipeTime = TimeBetweenSwipe;
// Save begin touch
_firstPressPos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
// Raycast down to see if you hit a vehicle and hold it
Ray screenRay = Camera.main.ScreenPointToRay(_firstPressPos);
RaycastHit rHit = new RaycastHit();
if (Physics.Raycast(screenRay, out rHit, 1000.0f, VehicleLayer.value, QueryTriggerInteraction.Collide))
{
// If you clicked a vehicle, save the reference to its behaviour
VehicleBehaviour vb = rHit.collider.GetComponentInParent<VehicleBehaviour>();
print(Vector3.Distance(Camera.main.transform.position, vb.gameObject.transform.position));
if (vb != null)
{
_vehicleBehaviour = vb;
}
else // clean the reference to the previous behaviour
{
_vehicleBehaviour = null;
}
// If you clicked a pedestrian, save the reference to its behaviour
PedestrianBehaviour pb = rHit.collider.GetComponentInParent<PedestrianBehaviour>();
if (pb != null && pb.IsCrossingOver == true)
{
_pedestrianBehaviour = pb;
_pedestrianBehaviour.Drag();
}
else // clean the reference to the previous behaviour
{
_pedestrianBehaviour = null;
}
}
else // Otherwise clean up both
{
_vehicleBehaviour = null;
_pedestrianBehaviour = null;
}
}
Does anyone have an idea?
Thanks in advance.
What happens to your FPS during these times? You should use the profiler.
FPS stays the same, there are no noticable drops in frames. As I stated in my question, nothing is out of the ordinary it just stops working sometimes.