/// <summary> /// Land the ship. /// </summary> public void Land() { if (currentState != ShipLanderState.Launched) { return; } RaycastHit hit; if (CheckCanLand(out hit)) { GetComponent <Rigidbody>().isKinematic = true; // Get the start and end positions for the landing animation startPos = transform.position; endPos = hit.point + hit.normal * landedHeight; // Get the start/end rotations for the landing animation startRot = transform.rotation; endRot = Quaternion.LookRotation(new Vector3(transform.forward.x, 0, transform.forward.z).normalized, hit.normal); currentState = ShipLanderState.Landing; currentStateStartTime = Time.time; onShipLanding.Invoke(); } }
/// <summary> /// Launch the ship. /// </summary> public void Launch() { if (currentState != ShipLanderState.Landed) { return; } GetComponent <Rigidbody>().isKinematic = true; // Get the start/end positions for the launching animation startPos = transform.position; endPos = transform.position + Vector3.up * (launchHeight - landedHeight); // Get the start/end rotations for the launching animation startRot = transform.rotation; endRot = transform.rotation; currentState = ShipLanderState.Launching; currentStateStartTime = Time.time; onShipLaunching.Invoke(); }
// Physics update void FixedUpdate() { switch (currentState) { // Launching animation case ShipLanderState.Launching: float amount = (Time.time - currentStateStartTime) / launchTime; if (amount >= 1) { amount = 1; GetComponent <Rigidbody>().isKinematic = false; currentState = ShipLanderState.Launched; currentStateStartTime = Time.time; onShipLaunched.Invoke(); } float curveAmount = launchCurve.Evaluate(amount); transform.position = curveAmount * endPos + (1 - curveAmount) * startPos; transform.rotation = Quaternion.Slerp(startRot, endRot, curveAmount); break; // Landing animation case ShipLanderState.Landing: amount = (Time.time - currentStateStartTime) / landTime; if (amount >= 1) { amount = 1; currentState = ShipLanderState.Landed; currentStateStartTime = Time.time; onShipLanded.Invoke(); } curveAmount = landCurve.Evaluate(amount); transform.position = curveAmount * endPos + (1 - curveAmount) * startPos; transform.rotation = Quaternion.Slerp(startRot, endRot, curveAmount); break; } }