Hey guys,
Im trying to to get my objects to deactivate when its a certain distance away from the player in x axis, which in my game is behind the player. the current code doesnt seem to work.
if(Vector3.Distance(transform.position, Player.playerSelf.transform.position) < 5)
{
switch(whoAmi)
{
case objType.obstacleA:
Manager.instance.obstacleA.ReleaseElement(gameObject, true);
break;
case objType.obstacleB:
Manager.instance.obstacleB.ReleaseElement(gameObject, true);
break;
case objType.obstacleC:
Manager.instance.obstacleC.ReleaseElement(gameObject, true);
break;
}
the problem is in the “if(Vector3.Distance(transform.position, Player.playerSelf.transform.position) < 5)”
but i cant work out what it should be?
If anyone could help i would appreciate it very much.
Thanks
try this if(Vector3.Distance(transform.position, Player.playerSelf.transform.position) > 5)
– Fornoreason1000thanks for the reply, ive tried that doesnt seem to work..
– Mustywhat exactly isnt working, is it throwing an error?
– violenceput this line after the { Debug.Log("Distance is more than 5!!!!!!!"); if you see something pop up in your console then then is nothing wrong with the code i posted. if you don't then one of the parameters might not be set right, namely the player.playerself. or you really aren't more than 5 metres away. is this a 2d game or something?
– Fornoreason1000well they are spawning, its just they get destroyed immediately because they start more than 5 meters away. this line basically ensures that they start at least 10 meters away (playerX.x + Random.Range(10.0f,40.0f)); try changing 5 to 50
– Fornoreason1000