t1 / TFDContents / Assets / KinectDemos / VisualizerDemo / Scripts / BallSpawner.cs @ 3
이력 | 보기 | 이력해설 | 다운로드 (1.81 KB)
1 |
using UnityEngine; |
---|---|
2 |
using System.Collections; |
3 |
|
4 |
public class BallSpawner : MonoBehaviour |
5 |
{ |
6 |
[Tooltip("Index of the player, tracked by this component. 0 means the 1st player, 1 - the 2nd one, 2 - the 3rd one, etc.")] |
7 |
public int playerIndex = 0; |
8 |
|
9 |
[Tooltip("Prefab used to instantiate balls in the scene.")] |
10 |
public Transform ballPrefab; |
11 |
|
12 |
[Tooltip("Prefab used to instantiate cubes in the scene.")] |
13 |
public Transform cubePrefab; |
14 |
|
15 |
[Tooltip("How many objects do we want to spawn.")] |
16 |
public int numberOfObjects = 20; |
17 |
|
18 |
private float nextSpawnTime = 0.0f; |
19 |
private float spawnRate = 1.5f; |
20 |
private int ballsCount = 0; |
21 |
|
22 |
|
23 |
void Update () |
24 |
{ |
25 |
if (nextSpawnTime < Time.time) |
26 |
{ |
27 |
SpawnBalls(); |
28 |
nextSpawnTime = Time.time + spawnRate; |
29 |
|
30 |
spawnRate = Random.Range(0f, 1f); |
31 |
//numberOfBalls = Mathf.RoundToInt(Random.Range(1f, 10f)); |
32 |
} |
33 |
} |
34 |
|
35 |
void SpawnBalls() |
36 |
{ |
37 |
KinectManager manager = KinectManager.Instance; |
38 |
|
39 |
if(ballPrefab && cubePrefab && ballsCount < numberOfObjects && |
40 |
manager && manager.IsInitialized() && manager.IsUserDetected(playerIndex)) |
41 |
{ |
42 |
long userId = manager.GetUserIdByIndex(playerIndex); |
43 |
Vector3 posUser = manager.GetUserPosition(userId); |
44 |
|
45 |
float xPos = Random.Range(-1.5f, 1.5f); |
46 |
float zPos = Random.Range(-1.5f, 1.5f); |
47 |
Vector3 spawnPos = new Vector3(posUser.x + xPos, posUser.y, posUser.z + zPos); |
48 |
|
49 |
int ballOrCube = Mathf.RoundToInt(Random.Range(0f, 1f)); |
50 |
|
51 |
Transform ballTransform = Instantiate(ballOrCube > 0 ? ballPrefab : cubePrefab, spawnPos, Quaternion.identity) as Transform; |
52 |
ballTransform.GetComponent<Renderer>().material.color = new Color(Random.Range(0.5f, 1f), Random.Range(0.5f, 1f), Random.Range(0.5f, 1f), 1f); |
53 |
ballTransform.parent = transform; |
54 |
|
55 |
ballsCount++; |
56 |
} |
57 |
} |
58 |
|
59 |
} |