Raycasts occasionally missing targets

I’m having a frustrating issue with raycasts…I have the following code as part of a special space invaders game. The idea is to look to the left, right, top, and bottom of an invader and check to see if there’s one within range, then check its color. This code seems to work about 90% of the time, but occasionally it just inexplicably misses. I was told it might be a ‘sync’ issue, but I’m not sure. Any help is appreciated.

int CheckBlockColor(GameObject TargetBlock)
	{
	
	RaycastHit hit;
	int counterB = 0;		
	TargetBlock.tag="Marked";
	

	// Check in all directions
		
	if (Physics.Raycast(TargetBlock.transform.position,Vector3.left, out hit, ray_length))
	{							
		if (hit.transform.CompareTag("Enemy"))
			if (hit.transform.GetComponent<Enemy_Script>().mycolor == hitcolor)
			{						
			hit.transform.tag = "Marked";
			counterB++;				
			}					
	}
	if (Physics.Raycast(TargetBlock.transform.position,Vector3.right, out hit, ray_length))
		if (hit.transform.CompareTag("Enemy"))
		    if (hit.transform.GetComponent<Enemy_Script>().mycolor == hitcolor)
			{					
			
			hit.transform.tag = "Marked";
			counterB++;
			}		
	if (Physics.Raycast(TargetBlock.transform.position,Vector3.up, out hit, ray_length))
	   if (hit.transform.CompareTag("Enemy"))
		    if (hit.transform.GetComponent<Enemy_Script>().mycolor == hitcolor)
			{		
			hit.transform.tag = "Marked";
			counterB++;
			}
	if (Physics.Raycast(TargetBlock.transform.position,Vector3.down, out hit, ray_length))
		if (hit.transform.CompareTag("Enemy"))
		    if (hit.transform.GetComponent<Enemy_Script>().mycolor == hitcolor)
			{		
			hit.transform.tag = "Marked";
			counterB++;
			}		
return counterB;		
	}

A synch problem in Unity is the same as anywhere else – things moving too fast, each frame, for their small size.

Suppose your guys have a 1 meter collider and are moving up at a speed of 25. If you move using 25*Time.deltaTime, then at 60fps you’re going 0.41 meters/frame. A picture, showing the checks just barely work:

^move 0.41 up^
-   ---            ---  --
|  |   |          |   | 0.5 clearance
1  |me |  ray --> |you| --
|  |   |          |   |
-   ---            ---

If “me” goes first, moves up 0.41 then checks, it will barely hit you’s top, by 0.09. If the frame rate drops to only 30fps, you’re now moving at 0.83 meters/frame. “Me” will shoot up too far, raycasting over you’s top, missing.

To test, drop your speed way down. If we move at 0.1 meters/frame (6M/S at 60fps) no matter who moves when, or if someone double-moves (end frame move, start frame move) it won’t matter.

Other things to maybe try: if you also have bullets or triggers, you could be hitting those – try printing hit.transform.name. Check in scene mode the green boxes (colliders) are staying with you. Try taking out the hitColor check – everything should get marked all the time. It’s possible the raycasts are fine and hitColor is getting set wrong. Make sure raylength isn’t just barely long enough (bad rounding/synch might make it stop just short.)

Thanks, all. Owen, I’m almost positive you identified the problem. However, I solved the issue by replacing the raycasts with a system based on arrays.