Question by
$$anonymous$$ · Aug 07, 2020 at 03:36 PM ·
c#logic
Problem with the logic in my code
I have 3 methods I use to spawn my object: private void CreateObstacle(float xPosition, float yPosition, bool bottom) { Quaternion squareRot = square.transform.rotation; squareRot = Quaternion.Euler(0f, 0f, rotAmt);
SpriteRenderer squareSprite = square.GetComponent<SpriteRenderer>();
Vector2 squareSize = squareSprite.size;
squareSize = new Vector2(OBSTACLE_WIDTH, OBSTACLE_HEIGHT);
Vector3 squarePos = square.transform.position;
// Vector3 squareScaleChange = new Vector3(2 * OBSTACLE_WIDTH, OBSTACLE_HEIGHT);
BoxCollider2D squareCol = square.GetComponent<BoxCollider2D>();
squareCol.size = squareSize;
if (bottom)
{
yPosition = -obstacleSpawnY;
}
else
{
yPosition = +obstacleSpawnY;
}
squarePos = new Vector3(xPosition, yPosition);
square = objectPooler.SpawnFromPool("Square", squarePos, squareRot);
Obstacle obstacle = new Obstacle(square, bottom);
obstacleList.Add(obstacle);
}
private void CreateObstacleUnit(float xPosition, float yPosition)
{
//gapSizepos = yPosition / 15f;
//gapSize = gapSizepos + gapSizepos;
// bottom
CreateObstacle(xPosition, yPosition, true);
// top
CreateObstacle(xPosition, yPosition, false);
obstacleSpawnCount++;
// SetDifficulty(GetDifficulty());
}
private void SpawnObstacleUnit()
{
obstacleSpawnTimer -= Time.deltaTime;
if (obstacleSpawnTimer < 0)
{
obstacleSpawnTimer = obstacleSpawnTimerMax;
float amtToChange = 9.5f;
// position = 2;
CreateObstacleUnit(obstacleSpawnX, obstacleSpawnY);
for (int i = 0; i < obstacleList.Count; i++)
{
Obstacle obstacle = obstacleList[i];
if (obstacle.IsBottom())
{
obstacleSpawnX += amtToChange;
obstacleSpawnY = 75f;
Debug.Log("Bottom: " + square.transform.position);
}
else
{
Debug.Log("Top: " + square.transform.position);
}
}
//SetPosition(GetPosition());
}
}
My problem occurs in the last method with if (obstacle.IsBottom()) { obstacleSpawnX += amtToChange; obstacleSpawnY = 75f; Debug.Log("Bottom: " + square.transform.position); } else { Debug.Log("Top: " + square.transform.position); } }
more specifically the debug statements. If bottom is true, the position should be a negative one, indicating the object is below the player. But, what's happening is that no matter if bottom is true or not, the y-position is the same as the top obstacle. A
Comment