Quantcast
Channel: Answers by "ransomink"
Viewing all articles
Browse latest Browse all 80

Answer by ransomink

$
0
0
1. Check if the player has collided with a spawned body 2. If so, spawn a new cube 3. After the cube is spawned, add it to the array spawnPoint on the CubeSpawner script is used as the position of the instantiate object. This was for test reasons; Obviously you'll use your own Vector3. CubeSpawner: #pragma strict // STATIC VARIABLES // public static var cs : CubeSpawner;// Reference to the CubeSpawner script // INSPECTOR VARIABLES // public var cube : GameObject;// GameObject used to spawn public var spawnPoint : Vector2;// Spawn position of the gameobject public var cubeArray : Array;// Array holding all spawned cubes // Used for initialization function Start () { cs = this;// Set cs as the reference to the CubeSpawner script // Create the cubeArray cubeArray = Array (); } // Spawn a new cube public function SpawnCube () { var newCube : GameObject; newCube = Instantiate ( cube, spawnPoint, transform.rotation ); // Add the spawned cube to an array cubeArray.Push ( newCube ); Debug.Log ( cubeArray ); } spawnedBody on the CheckTrigger script should be the spawned bodies you spoke of in your game. CheckTrigger: #pragma strict // Check when a gameobject has entered the trigger function OnTriggerEnter ( col : Collider ) { // If the collided object is a spawned body... if ( col.gameObject == spawnedBody ) { // ... spawn a new cube, add it to the array, and destroy the collided gameobject CubeSpawner.cs.SpawnCube (); //Destroy ( col.gameObject ); } } If you have multiple spawned bodies, there are 2 methods (I can think of now) that you can use. Method #1: Put all spawned bodies in an array. Inside of the trigger function, check through each gameobject in that array, and compare that gameobject to the collided gameobject. If they match, then spawn a new cube. Method #2: Give each spawned body a tag called 'SpawnedBody' (without quotes). Inside of the trigger function, compare the collided gameobjects tag to the spawned bodies tag. If they match, spawn a new cube. This method is cleaner and seems easier to do than method 1. p.s. Do not use arrays, specially in Unityscript; Instead, use a List. Here are some great references for learning what list are and how to use them. [http://answers.unity3d.com/questions/198318/javascript-array-use-with-a-struct-.html][1] [http://answers.unity3d.com/questions/327065/explanation-on-how-to-use-list-in-unityscript.html][2] [http://msdn.microsoft.com/en-us/library/d9hw1as6%28v=vs.90%29.aspx][3] [1]: http://answers.unity3d.com/questions/198318/javascript-array-use-with-a-struct-.html [2]: http://answers.unity3d.com/questions/327065/explanation-on-how-to-use-list-in-unityscript.html [3]: http://msdn.microsoft.com/en-us/library/d9hw1as6%28v=vs.90%29.aspx

Viewing all articles
Browse latest Browse all 80

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>