IEnumerator ComputeDistance() { CollosionTest collosionTest = this.GetComponent <CollosionTest>(); while (true) { if (thisTransform.position.x <= tempX[0] + 1) //1为半径差 { StartCoroutine(Bounce(thisTransform.right * bounceSpeed)); } else if (thisTransform.position.x >= tempX[1] - 1) //1为半径差 { StartCoroutine(Bounce(-thisTransform.right * bounceSpeed)); } if (thisTransform.position.z <= tempZ[0] + 1) //1为半径差 { StartCoroutine(Bounce(thisTransform.forward * bounceSpeed)); } else if (thisTransform.position.z >= tempZ[1] - 1) //1为半径差 { StartCoroutine(Bounce(-thisTransform.forward * bounceSpeed)); } yield return(null); } }
private void Start() { thisTransform = this.transform; collosionTest = this.GetComponent <CollosionTest>(); for (int i = 0; i < wallTransforms.Length; i++) { if (wallTransforms[i].position.x != 0) { if (wallTransforms[i].position.x < 0) { tempX[0] = wallTransforms[i].position.x; } else if (wallTransforms[i].position.x > 0) { tempX[1] = wallTransforms[i].position.x; } } else if (wallTransforms[i].position.z != 0) { if (wallTransforms[i].position.z < 0) { tempZ[0] = wallTransforms[i].position.z; } else if (wallTransforms[i].position.z > 0) { tempZ[1] = wallTransforms[i].position.z; } } } StartCoroutine(ComputeDistance()); }