Raycast works perfect, except the first time it is executed

Hello,

I’ve been working on the following code snippet for a long time, and am reaching out to see if there is anything obvious I may have missed. This is for a grid-based turn-based strategy game. The code below is run each time a character changes tiles. In a nutshell, it first makes all enemy units invisible (which turns their playerCanSee flag to false), then goes through each player unit in the battle looking for each enemy unit, and assuming they can be seen, it makes them visible.

This code works flawlessly, with the exception of the first time it is run. This does not have to do with the Start() function ordering, as I can run it manually several seconds into gameplay and it still will not work. Subsequent times, it works perfectly. I know I could just make it run twice at runtime, but I really want to understand why this is the case so I can learn better.

*Clarification: By it not working properly, I mean that the raycast goes directly through the intended target. It does, however, hit targets behind it. I’ve played with disabling the layer change to no effect.

function DetermineLOSAllUnits()
{
	for(var i=0;i<enemyUnitsInBattle.length;i++)
	{
		var enemyUnit : GameObject = enemyUnitsInBattle*;*
  •  enemyUnit.GetComponent(unitScript).MakeInvisible();*
    
  • }*
  • for(var j=0;j<playerUnitsInBattle.length;j++)*
  • {*
  •  var curPlayerUnit : GameObject = playerUnitsInBattle[j];*
    
  •  var rayStart : Vector3 = curPlayerUnit.GetComponent(unitScript).currentLocation.transform.position;*
    
  •  rayStart.y = curPlayerUnit.GetComponent(unitScript).currentLocation.GetComponent(blockScript).blockTop + curPlayerUnit.GetComponent(unitScript).height;*
    
  •  for(var k=0;k<enemyUnitsInBattle.length;k++)*
    
  •  {*
    
  •  	var curEnemyUnit : GameObject = enemyUnitsInBattle[k];*
    
  •  	if(curEnemyUnit.GetComponent(unitScript).playerCanSee == false)*
    
  •  	{*
    
  •  		curEnemyUnit.GetComponent(unitScript).currentLocation.GetComponent(blockScript).raycastTesterOnTile.transform.position.y =*
    
  •  		curEnemyUnit.GetComponent(unitScript).currentLocation.GetComponent(blockScript).blockTop + curEnemyUnit.GetComponent(unitScript).height;*
    
  •  		curEnemyUnit.GetComponent(unitScript).currentLocation.GetComponent(blockScript).raycastTesterOnTile.layer = 0;*
    
  •  		var direction : Vector3 = curEnemyUnit.GetComponent(unitScript).currentLocation.GetComponent(blockScript).raycastTesterOnTile.transform.position - rayStart;*
    
  •  		var hit : RaycastHit;*
    
  •  		if(Physics.Raycast(rayStart, direction, hit, 30))*
    
  •  		{*
    
  •  			Debug.DrawLine(rayStart,hit.point,Color.blue,3,true);*
    
  •  			if(hit.transform.gameObject == curEnemyUnit.GetComponent(unitScript).currentLocation.GetComponent(blockScript).raycastTesterOnTile)*
    
  •  			{*
    
  •  				curEnemyUnit.GetComponent(unitScript).MakeVisible();*
    
  •  			}*
    
  •  		}*
    
  •  		curEnemyUnit.GetComponent(unitScript).currentLocation.GetComponent(blockScript).raycastTesterOnTile.layer = 2;*
    
  •  	}*
    
  •  }*
    
  • }*
    }
    Thanks in advance! If anything is unclear please let me know and I will do my best to clarify.

I figured it out, it looks like the raycast was happening before the raycastTester object moved. Not sure if that is working as intended or not, but my solution was to align the raycastTester at runtime and whenever a unit moves.

If you figured it out, would you be so kind as to closing the question do other people, like me, don't come on here, read your question and then realise you already answered it yourself!

Took me a while to figure out how, but thanks for letting me know I needed to do that.

1 Answer

1

This has been answered