Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Problem with moving prefab clones

Discussion in 'Scripting' started by Joralin, Sep 2, 2014.

  1. Joralin

    Joralin

    Joined:
    Aug 27, 2014
    Posts:
    30
    Hi,


    I have Gameobjectst and made prefabs out of them. But i have a problem moving them with "lerp". I think it has something to do with "instantinating" . I used nearly the same functionality in another script and there it works.


    I want to spawn "Instances" of my prefab Objects. One Instance every x seconds.


    The "Spawn" function is working fine. The problem ist the "move" function. The Objects are spawning at the correct position, but "lerp" move them instantly some units away. I figured out that this is because "beginningTime" is always set to zero after the function "move" started. But i have no idea why this is happening all the time.


    Here an example how my Code looks like:


    // GameMaster Class, here i manage the whole game:

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var crebse : GameObject;
    4. var actualInsect : GameObject;
    5. var spawnTimer : float;
    6. var firstTime : float;
    7. var randomNum : int;
    8. public static var insectsArray        = new Array();
    9.    
    10. function Start ()
    11. {
    12.     spawnTimer = 0.0;
    13.     crabs = GameObject.FindGameObjectWithTag("Crabs_Prefab");
    14.     firstTime = Time.time;
    15. }
    16.  
    17. function Update () {
    18.  
    19.     spawnInsects ();
    20.      moveInsects ();
    21. }
    22.     function spawnInsects ()
    23.     {
    24.         var secondTime = Time.time;
    25.        
    26.         // Spawn all 3 Seconds an Insect
    27.         if( secondTime - firstTime > 3.0)
    28.         {
    29.             firstTime = Time.time;
    30.             crabs.GetComponent(WaterInsects_Script).Spawn();
    31.         }
    32.     }
    33.  
    34.  
    35. function moveInsects ()
    36. {
    37.     // go through all created instances and move them
    38.     for (var i: int  = 0; i < insectsArray .length; i++)
    39.     {  
    40.         actualInsect = insectsArray[i];
    41.         var Insectnumber = i;
    42.         actualInsect.GetComponent(WaterInsects_Script).Move();
    43.     }
    44. }
    45.  
    46.  
    47. //The Script "WaterInsects_Script" that is attached to the "Prefab Objects":
    48.  
    49.  
    50. #pragma strict
    51.  
    52. private var spawnInsect : GameObject;
    53.  
    54. var movementSpeed         : float;
    55. var distCovered             : float;
    56. var fracJourney             : float;
    57. var timeAtSpawn             : float;
    58. var journeyLength             : float;
    59.  
    60. function Start ()
    61. {
    62. }
    63. function Update () {
    64. }
    65. function Spawn ()
    66. {
    67.     var newWaterAnimal = this.transform.gameObject;
    68.     journeyLength = Vector2.Distance(Vector2 (0, 2), Vector2 (5, 5));
    69.  
    70.     spawnInsect = Instantiate (newWaterAnimal, Vector2 (0, 2), Quaternion.identity ) as     GameObject ;
    71.     GameMaster_Class.InsectsArray.push(spawnInsect);
    72.    
    73.     // The is variable is set when an object spawns
    74.     timeAtSpawn = Time.time;
    75. }
    76.  
    77. function Move ()
    78. {
    79.  
    80. // calculate the actual moved way            
    81. distCovered = (Time.time -timeAtSpawn)* movementSpeed;
    82.  
    83. // calculate the actual percentage of the moved way          
    84. fracJourney = distCovered / journeyLength;
    85.  
    86. // move the object with lerp
    87. gameObject.transform.position = Vector3.Lerp(Vector2 (0, 2), Vector2 (5, 5), fracJourney);  
    88. }
    Im Sorry that this is a little bit messy, i copied some parts out and changed some names.
     
  2. Joralin

    Joralin

    Joined:
    Aug 27, 2014
    Posts:
    30
    Ok, i dont know how but i think i got it to work now. But need still to do some test if it really does Oo