1
0

FlowerPlanted.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. #if UNITY_URP
  36. Color newColor = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);
  37. newColor.a = 0.75f;
  38. planting.GetComponentInChildren<MeshRenderer>().material.SetColor("_BaseColor", newColor);
  39. #else
  40. planting.GetComponentInChildren<MeshRenderer>().material.SetColor("_TintColor", Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f));
  41. #endif
  42. Rigidbody rigidbody = planting.GetComponent<Rigidbody>();
  43. if (rigidbody != null)
  44. rigidbody.isKinematic = true;
  45. Vector3 initialScale = Vector3.one * 0.01f;
  46. Vector3 targetScale = Vector3.one * (1 + (Random.value * 0.25f));
  47. float startTime = Time.time;
  48. float overTime = 0.5f;
  49. float endTime = startTime + overTime;
  50. while (Time.time < endTime)
  51. {
  52. planting.transform.localScale = Vector3.Slerp(initialScale, targetScale, (Time.time - startTime) / overTime);
  53. yield return null;
  54. }
  55. if (rigidbody != null)
  56. rigidbody.isKinematic = false;
  57. }
  58. }
  59. }