using System.Collections; using System.Collections.Generic; using UnityEngine; public class SphereController : MonoBehaviour { private Vector3 RespawnPoint; private Quaternion RespawnOrientation; bool isReplace; private Rigidbody _rigidbody; BoxCollider box; public float _force = 1; private Vector3 direction; Vector3 _tempPos=Vector3.zero; bool isThrow = false; List posList=new List(); // Start is called before the first frame update void Start() { _rigidbody = GetComponent(); box = GetComponent(); RespawnPoint = this.transform.position; RespawnOrientation = this.transform.rotation; } private void Update() { } public void Throwing() { if (box.enabled==true) { box.enabled = false; } _tempPos = this.transform.position; posList.Add(_tempPos); } public void ThrowEnd() { isThrow = true; } void LateUpdate() { if (isThrow) { AddForce(); isThrow = false; } } public void AddForce() { if (posList.Count > 9) { direction = (posList[posList.Count - 1] - posList[posList.Count - 5]).normalized; Debug.Log(direction + "——————" + posList.Count); _rigidbody.AddForce(direction * _force, ForceMode.Force); } posList.Clear(); } private void replace() { if (_rigidbody != null) { _rigidbody.velocity = Vector3.zero; _rigidbody.angularVelocity = Vector3.zero; } this.transform.SetPositionAndRotation(RespawnPoint, RespawnOrientation); if (box.enabled==false) { box.enabled = true; } isReplace = false; Debug.Log("LGS: RePlace"); } public void OnCollisionEnter(Collision collision) { //if (collision.gameObject.tag == "collision") //{ // Debug.Log(collision.gameObject.name); // direction = (this.transform.position - collision.transform.position).normalized; // _rigidbody.AddForce(direction * _force, ForceMode.Force); //} //else if(collision.gameObject.tag=="floor" && isReplace==false) { Invoke("replace", 2f); isReplace = true; } } public void OnCollisionStay(Collision collision) { } public void OnCollisionExit(Collision collision) { } }