Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. namespace GoogleVR.HelloVR {
  2. using UnityEngine;
  3.  
  4. [RequireComponent(typeof(Collider))]
  5. public class ObjectController : MonoBehaviour {
  6. private Vector3 startingPosition;
  7. private Renderer renderer;
  8.  
  9. public Material inactiveMaterial;
  10. public Material gazedAtMaterial;
  11.  
  12. void Start() {
  13. startingPosition = transform.localPosition;
  14. renderer = GetComponent<Renderer>();
  15. SetGazedAt(false);
  16. }
  17.  
  18. public void SetGazedAt(bool gazedAt) {
  19. if (inactiveMaterial != null && gazedAtMaterial != null) {
  20. renderer.material = gazedAt ? gazedAtMaterial : inactiveMaterial;
  21. return;
  22. }
  23. }
  24.  
  25. public void Reset() {
  26. int sibIdx = transform.GetSiblingIndex();
  27. int numSibs = transform.parent.childCount;
  28. for (int i=0; i<numSibs; i++) {
  29. GameObject sib = transform.parent.GetChild(i).gameObject;
  30. sib.transform.localPosition = startingPosition;
  31. sib.SetActive(i == sibIdx);
  32. }
  33. }
  34.  
  35. public void Recenter() {
  36. #if !UNITY_EDITOR
  37. GvrCardboardHelpers.Recenter();
  38. #else
  39. if (GvrEditorEmulator.Instance != null) {
  40. GvrEditorEmulator.Instance.Recenter();
  41. }
  42. #endif // !UNITY_EDITOR
  43. }
  44.  
  45. public void TeleportRandomly() {
  46. // Pick a random sibling, move them somewhere random, activate them,
  47. // deactivate ourself.
  48. int sibIdx = transform.GetSiblingIndex();
  49. int numSibs = transform.parent.childCount;
  50. sibIdx = (sibIdx + Random.Range(1, numSibs)) % numSibs;
  51. GameObject randomSib = transform.parent.GetChild(sibIdx).gameObject;
  52.  
  53. // Move to random new location ±100º horzontal.
  54. Vector3 direction = Quaternion.Euler(
  55. 0,
  56. Random.Range(-90, 90),
  57. 0) * Vector3.forward;
  58. // New location between 1.5m and 3.5m.
  59. float distance = 2 * Random.value + 1.5f;
  60. Vector3 newPos = direction * distance;
  61. // Limit vertical position to be fully in the room.
  62. newPos.y = Mathf.Clamp(newPos.y, -1.2f, 4f);
  63. randomSib.transform.localPosition = newPos;
  64.  
  65. randomSib.SetActive(true);
  66. gameObject.SetActive(false);
  67. SetGazedAt(false);
  68. }
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement