SphereController.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class SphereController : MonoBehaviour
  5. {
  6. private Vector3 RespawnPoint;
  7. private Quaternion RespawnOrientation;
  8. bool isReplace;
  9. private Rigidbody _rigidbody;
  10. BoxCollider box;
  11. public float _force = 1;
  12. private Vector3 direction;
  13. Vector3 _tempPos=Vector3.zero;
  14. bool isThrow = false;
  15. List<Vector3> posList=new List<Vector3>();
  16. // Start is called before the first frame update
  17. void Start()
  18. {
  19. _rigidbody = GetComponent<Rigidbody>();
  20. box = GetComponent<BoxCollider>();
  21. RespawnPoint = this.transform.position;
  22. RespawnOrientation = this.transform.rotation;
  23. }
  24. private void Update()
  25. {
  26. }
  27. public void Throwing()
  28. {
  29. if (box.enabled==true)
  30. {
  31. box.enabled = false;
  32. }
  33. _tempPos = this.transform.position;
  34. posList.Add(_tempPos);
  35. }
  36. public void ThrowEnd()
  37. {
  38. isThrow = true;
  39. }
  40. void LateUpdate()
  41. {
  42. if (isThrow)
  43. {
  44. AddForce();
  45. isThrow = false;
  46. }
  47. }
  48. public void AddForce()
  49. {
  50. if (posList.Count > 9)
  51. {
  52. direction = (posList[posList.Count - 1] - posList[posList.Count - 5]).normalized;
  53. Debug.Log(direction + "——————" + posList.Count);
  54. _rigidbody.AddForce(direction * _force, ForceMode.Force);
  55. }
  56. posList.Clear();
  57. }
  58. private void replace()
  59. {
  60. if (_rigidbody != null)
  61. {
  62. _rigidbody.velocity = Vector3.zero;
  63. _rigidbody.angularVelocity = Vector3.zero;
  64. }
  65. this.transform.SetPositionAndRotation(RespawnPoint, RespawnOrientation);
  66. if (box.enabled==false)
  67. {
  68. box.enabled = true;
  69. }
  70. isReplace = false;
  71. Debug.Log("LGS: RePlace");
  72. }
  73. public void OnCollisionEnter(Collision collision)
  74. {
  75. //if (collision.gameObject.tag == "collision")
  76. //{
  77. // Debug.Log(collision.gameObject.name);
  78. // direction = (this.transform.position - collision.transform.position).normalized;
  79. // _rigidbody.AddForce(direction * _force, ForceMode.Force);
  80. //}
  81. //else
  82. if(collision.gameObject.tag=="floor" && isReplace==false)
  83. {
  84. Invoke("replace", 2f);
  85. isReplace = true;
  86. }
  87. }
  88. public void OnCollisionStay(Collision collision)
  89. {
  90. }
  91. public void OnCollisionExit(Collision collision)
  92. {
  93. }
  94. }