- Home /
How to detect if transform.position is not vector3 + 100 on x position
Hello, what I want to do is that when the position of the player is equal to the previous position only that in the x I advance 10
This is my script but it doesnt work:
public GameObject[] posibleIslands;
public GameObject IslandPrefab;
public float Range1;
public float Range2;
public float Range3;
public float Range4;
public float Range5;
public float Range6;
private float valueX;
private float valueY;
private float valueZ;
public Vector3 myPosition;
public Vector3 myOldPosition;
public Vector3 pos;
void Update ()
{
pos = new Vector3(valueX, valueY, valueZ);
valueX = Random.Range (Range1, Range2);
valueY = Random.Range (Range3, Range4);
valueZ = Random.Range (Range5, Range6);
myPosition = transform.position;
if (myPosition == myOldPosition + new Vector3(10, 0, 0) || myPosition == myOldPosition - new Vector3(10, 0, 0) || myPosition == myOldPosition + new Vector3(0, 0, 10) || myPosition == myOldPosition - new Vector3(0, 0, 10))
{
for (int i = 0; i < Random.Range (1, 5); i++)
{
GenerateIsland ();
i = 0;
}
myOldPosition = myPosition;
}
}
public void GenerateIsland()
{
int posibleIslandsInt = Random.Range (0, posibleIslands.Length);
if (posibleIslandsInt == 0)
{
IslandPrefab = posibleIslands [0];
}
if (posibleIslandsInt == 1)
{
IslandPrefab = posibleIslands [1];
}
if (posibleIslandsInt == 2)
{
IslandPrefab = posibleIslands [2];
}
Instantiate(IslandPrefab, pos, Quaternion.identity);
}
Answer by ransomink · Mar 06, 2018 at 11:24 AM
pos = new Vector3( valueX, valueY, valueZ );
First, you set the pos
variable based on your valueX, valueY, and valueZ but they are private (not accessible in the inspector and are changed after setting the variable, so pos will be (0, 0, 0)
valueX = Random.Range( Range1, Range2 );
valueY = Random.Range( Range3, Range4 );
valueZ = Random.Range( Range5, Range6 );
These variables are used on the previous line but set afterwards. The random generated numbers will not be used until the next frame, so this does nothing
if ( myPosition == myOldPosition + new Vector3( 10, 0, 0 ) )
Do you want to check if your current position is your old position + 10, or do you want to move 10 if the current position is the old position?
for ( int i = 0; i < Random.Range( 1, 5 ); i++ )
{
GenerateIsland();
i = 0;
}
This code block doesn't make sense. You're comparing i to a random number every loop so it can skip or repeat i but inside the loop you're resetting i back to 0. this will make i return 0 or 1 and nothing else. What type of behaviour are you trying to achieve?