/** * OnTriggerEnter is called then two Colliders collide. * If the Ship collides and was not hit before (isAlive == true) then disable all renderers to make the ship invisible. * The Ship cannot move, when it was hit, so set canMove to false. Set isAlive to false, so it cannot be hit a second time. * Then disable the IonCannon, as the Ship cannot shoot, when it was hit. * Lastly call Reset(), which will reset the Ship to the start. */ /*public void OnTriggerEnter(Collider collider) { * if (isAlive) { * SkinnedMeshRenderer[] renderers = GetComponentsInChildren<SkinnedMeshRenderer>(); * foreach (SkinnedMeshRenderer renderer in renderers) { * renderer.enabled = false; * } * canMove = false; * isAlive = false; * IonCannon ionCannon = gameObject.GetComponent<IonCannon>(); * if (ionCannon) { * ionCannon.SetCanShoot(false); * } * } * StartCoroutine (Reset()); * }*/ /** * Resets the Ship to its start position after 2 seconds. This is done with Coroutines and WaitForSeconds. * After the two seconds all renderers are enabled. The moveDirection is set to (0, 0, 0), so that the ship stands still, when it reappears. * canMove and isAlive are set to true. * Lastly the sthip rotation and position are set to their initial values. */ public IEnumerator Reset() { yield return(new WaitForSeconds(2)); SkinnedMeshRenderer[] renderers = GetComponentsInChildren <SkinnedMeshRenderer>(); foreach (SkinnedMeshRenderer renderer in renderers) { renderer.enabled = true; } moveDirection = new Vector3(); canMove = true; isAlive = true; IonCannon ionCannon = gameObject.GetComponent <IonCannon>(); ionCannon.SetCanShoot(true); transform.position = startPosition; transform.rotation = startRotation; }
/** * OnTriggerEnter is called when two Colliders collide. * If the Ship collides and was not hit before (isAlive == true) then disable all renderers to make the ship invisible. * The Ship cannot move when it is hit, so set canMove to false. Set isAlive to false, so it cannot be hit a second time. * Then disable the IonCannon, as the Ship cannot shoot when it is hit. * Lastly call Reset(), which will reset the Ship to the start. */ public void OnTriggerEnter(Collider collider) { if (isAlive) { SkinnedMeshRenderer[] renderers = GetComponentsInChildren <SkinnedMeshRenderer>(); foreach (SkinnedMeshRenderer renderer in renderers) { renderer.enabled = false; } canMove = false; isAlive = false; IonCannon ionCannon = gameObject.GetComponent <IonCannon>(); if (ionCannon) { ionCannon.SetCanShoot(false); } } StartCoroutine(Reset()); }