FlowerPlanted.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using Valve.VR.InteractionSystem;
  6. namespace Valve.VR.InteractionSystem.Sample
  7. {
  8. public class FlowerPlanted : MonoBehaviour
  9. {
  10. private void Start()
  11. {
  12. Plant();
  13. }
  14. public void Plant()
  15. {
  16. StartCoroutine(DoPlant());
  17. }
  18. private IEnumerator DoPlant()
  19. {
  20. Vector3 plantPosition;
  21. RaycastHit hitInfo;
  22. bool hit = Physics.Raycast(this.transform.position, Vector3.down, out hitInfo);
  23. if (hit)
  24. {
  25. plantPosition = hitInfo.point + (Vector3.up * 0.05f);
  26. }
  27. else
  28. {
  29. plantPosition = this.transform.position;
  30. plantPosition.y = Player.instance.transform.position.y;
  31. }
  32. GameObject planting = this.gameObject;
  33. planting.transform.position = plantPosition;
  34. planting.transform.rotation = Quaternion.Euler(0, Random.value * 360f, 0);
  35. planting.GetComponentInChildren<MeshRenderer>().material.SetColor("_TintColor", Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f));
  36. Rigidbody rigidbody = planting.GetComponent<Rigidbody>();
  37. if (rigidbody != null)
  38. rigidbody.isKinematic = true;
  39. Vector3 initialScale = Vector3.one * 0.01f;
  40. Vector3 targetScale = Vector3.one * (1 + (Random.value * 0.25f));
  41. float startTime = Time.time;
  42. float overTime = 0.5f;
  43. float endTime = startTime + overTime;
  44. while (Time.time < endTime)
  45. {
  46. planting.transform.localScale = Vector3.Slerp(initialScale, targetScale, (Time.time - startTime) / overTime);
  47. yield return null;
  48. }
  49. if (rigidbody != null)
  50. rigidbody.isKinematic = false;
  51. }
  52. }
  53. }