t1 / TFDContents / Assets / KinectDemos / BackgroundRemovalDemo / Scripts / UserPlaneMover.cs @ 3
이력 | 보기 | 이력해설 | 다운로드 (1.58 KB)
| 1 | 3 | KTH | using UnityEngine; |
|---|---|---|---|
| 2 | using System.Collections; |
||
| 3 | |||
| 4 | public class UserPlaneMover : MonoBehaviour |
||
| 5 | {
|
||
| 6 | [Tooltip("Index of the player, tracked by this component. 0 - the 1st player, 1 - the 2nd player, etc.")]
|
||
| 7 | public int playerIndex = 0; |
||
| 8 | |||
| 9 | // [Tooltip("Whether the user position is calculated relative to the initial user position.")]
|
||
| 10 | // public bool relToInitialPos = false; |
||
| 11 | |||
| 12 | [Tooltip("Smooth factor used for the transform movement.")]
|
||
| 13 | public float smoothFactor = 20f; |
||
| 14 | |||
| 15 | |||
| 16 | private KinectManager manager = null; |
||
| 17 | private long lastUserId = 0; |
||
| 18 | |||
| 19 | private long userId = 0; |
||
| 20 | private Vector3 initialPlanePos = Vector3.zero; |
||
| 21 | // private Vector3 initialUserPos = Vector3.zero; |
||
| 22 | private Vector3 currentUserPos = Vector3.zero; |
||
| 23 | |||
| 24 | |||
| 25 | void Start () |
||
| 26 | {
|
||
| 27 | manager = KinectManager.Instance; |
||
| 28 | initialPlanePos = transform.position; |
||
| 29 | } |
||
| 30 | |||
| 31 | void Update () |
||
| 32 | {
|
||
| 33 | if (manager == null || !manager.IsInitialized()) |
||
| 34 | return; |
||
| 35 | |||
| 36 | userId = manager.GetUserIdByIndex(playerIndex); |
||
| 37 | currentUserPos = manager.GetUserPosition(userId); |
||
| 38 | |||
| 39 | if (userId != 0 && userId != lastUserId) |
||
| 40 | {
|
||
| 41 | lastUserId = userId; |
||
| 42 | // initialUserPos = currentUserPos; |
||
| 43 | } |
||
| 44 | |||
| 45 | if (userId != 0) |
||
| 46 | {
|
||
| 47 | Vector3 deltaUserPos = currentUserPos; // relToInitialPos ? (currentUserPos - initialUserPos) : currentUserPos; |
||
| 48 | Vector3 newPlanePos = initialPlanePos + new Vector3(0f, 0f, deltaUserPos.z); |
||
| 49 | |||
| 50 | transform.position = Vector3.Lerp(transform.position, newPlanePos, smoothFactor * Time.deltaTime); |
||
| 51 | } |
||
| 52 | else |
||
| 53 | {
|
||
| 54 | lastUserId = 0; |
||
| 55 | // initialUserPos = Vector3.zero; |
||
| 56 | |||
| 57 | transform.position = initialPlanePos; |
||
| 58 | } |
||
| 59 | |||
| 60 | } |
||
| 61 | |||
| 62 | } |