t1 / TFDContents / Assets / KinectDemos / ColliderDemo / Scripts / EggSpawner.cs @ 3
이력 | 보기 | 이력해설 | 다운로드 (1.21 KB)
1 | 3 | KTH | using UnityEngine; |
---|---|---|---|
2 | using System.Collections; |
||
3 | |||
4 | public class EggSpawner : 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 (model and components) used to instantiate eggs in the scene.")] |
||
10 | public Transform eggPrefab; |
||
11 | |||
12 | private float nextEggTime = 0.0f; |
||
13 | private float spawnRate = 1.5f; |
||
14 | |||
15 | void Update () |
||
16 | { |
||
17 | if (nextEggTime < Time.time) |
||
18 | { |
||
19 | SpawnEgg(); |
||
20 | nextEggTime = Time.time + spawnRate; |
||
21 | |||
22 | spawnRate = Mathf.Clamp(spawnRate, 0.3f, 99f); |
||
23 | } |
||
24 | } |
||
25 | |||
26 | void SpawnEgg() |
||
27 | { |
||
28 | KinectManager manager = KinectManager.Instance; |
||
29 | |||
30 | if(eggPrefab && manager && manager.IsInitialized() && manager.IsUserDetected(playerIndex)) |
||
31 | { |
||
32 | long userId = manager.GetUserIdByIndex(playerIndex); |
||
33 | Vector3 posUser = manager.GetUserPosition(userId); |
||
34 | |||
35 | float addXPos = Random.Range(-2f, 2f); |
||
36 | Vector3 spawnPos = new Vector3(addXPos, 5f, posUser.z - 0.1f); |
||
37 | |||
38 | Transform eggTransform = Instantiate(eggPrefab, spawnPos, Quaternion.identity) as Transform; |
||
39 | eggTransform.parent = transform; |
||
40 | } |
||
41 | } |
||
42 | |||
43 | } |