Search Unity

Falling objects

Discussion in 'General Discussion' started by MaciejKusztal, Mar 29, 2020.

?

How can you make the object return to its original position by adding a condition for the y position

  1. The object accepts the condition of position y and returns to the initial position

    1 vote(s)
    100.0%
  2. The object accepts the condition of position y and returns to the random position x and initial y

    1 vote(s)
    100.0%
Multiple votes are allowed.
  1. MaciejKusztal

    MaciejKusztal

    Joined:
    Feb 4, 2020
    Posts:
    87
    Hello,
    I'm going to create a background on which objects that fall are appearing. This has been achieved, the object (currently one) this, however, after reaching the value of y (-224) should be created again, or simply to the initial position. However, this does not happen I have already tried many conditions, however, nothing gives the intended effect.
    I am asking for help in solving this puzzle.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SquareBackground : MonoBehaviour
    6. {
    7.     [SerializeField]
    8.     private float xLimit;
    9.  
    10.     [SerializeField]
    11.     private float[] xPosition;
    12.  
    13.     [SerializeField]
    14.     private Wave[] wave;
    15.  
    16.     private float currentTime;
    17.  
    18.     List<float> remainingPositions = new List<float>();
    19.     private int waveIndex;
    20.     float xPos = 0;
    21.     int rand;
    22.  
    23.     void Start()
    24.     {
    25.         currentTime = 0;
    26.         remainingPositions.AddRange(xPosition);
    27.     }
    28.  
    29.     void Update()
    30.     {
    31.         if(transform.position.y == -224)
    32.         {
    33.             SelectWave();
    34.             fallObject(xPos);
    35.         }
    36.     }
    37.  
    38.     void fallObject(float xPos)
    39.     {
    40.         int r = 0;
    41.         string objectName = "";
    42.         if (r == 0)
    43.             objectName = "fallSquare";
    44.  
    45.         GameObject fallingObject = PollingObjects.instance.GetPooledObject(objectName);
    46.         fallingObject.transform.position = new Vector3(xPos, transform.position.y, 0);
    47.         fallingObject.SetActive(true);
    48.     }
    49.  
    50.     void SelectWave()
    51.     {
    52.         remainingPositions = new List<float>();
    53.         remainingPositions.AddRange(xPosition);
    54.  
    55.         waveIndex = Random.Range(0, wave.Length);
    56.  
    57.         currentTime = wave[waveIndex].delayTime;
    58.  
    59.         if (wave[waveIndex].spawnAmount == 1)
    60.             xPos = Random.Range(-xLimit, xLimit);
    61.         else if(wave[waveIndex].spawnAmount > 1)
    62.         {
    63.             rand = Random.Range(0, remainingPositions.Count);
    64.             xPos = remainingPositions[rand];
    65.             remainingPositions.RemoveAt(rand);
    66.         }
    67.  
    68.         for(int i = 0; i < wave[waveIndex].spawnAmount; i++)
    69.         {
    70.             fallObject(xPos);
    71.             rand = Random.Range(0, remainingPositions.Count);
    72.             xPos = remainingPositions[rand];
    73.             remainingPositions.RemoveAt(rand);
    74.         }
    75.     }
    76. }
    77.  
    78. [System.Serializable]
    79. public class Wave
    80. {
    81.     public float delayTime;
    82.     public float spawnAmount;
    83. }
    The PollingObject class is responsible for copying the object.
    The most significant functions are "Update ()" and "fallObject (float)".
     
  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,566
    Here's the problem:
    Code (csharp):
    1.  
    2. if(transform.position.y == -224)
    3.  
    Position.y is a floating point value and it will never be exactly equal to -224.
    Use "<= -224" instead.

    Additionally, the logic feels overcomplicated here.
     
  3. MaciejKusztal

    MaciejKusztal

    Joined:
    Feb 4, 2020
    Posts:
    87
    Yes, you were right. Swapping the sign helped.
    I added some parameter changes and the puzzle has been solved.
    I just have a question why the parameters x and y shown in unity are different from those in the code?
     
  4. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,566
    Because numbers are floating points. Not all decimal numbers can be precisely represented, and as a result most programs that display floating points round them off to nearest value, based on precision.

    The most likely scenario is that coordinate was -223.9999999 or something like that.

    Aside from using <= instead of == another option is this function:
    https://docs.unity3d.com/ScriptReference/Mathf.Approximately.html