Пример #1
0
    void Update()
    {
        if (fc)
        {
            if (fc.interplanetaryFlight && Input.GetButtonDown("Orbit"))
            {
                fc.interplanetaryFlight = false;
                entryVelocity           = fc.rb.velocity;
                shipEntryHeading        = entryVelocity.normalized;

                angular = Quaternion.FromToRotation(
                    fc.transform.forward,
                    shipEntryHeading
                    ).eulerAngles.magnitude;

                //Debug.Log(angular);

                fc.rb.Sleep();

                Vector3 planetToShipDirection = (fc.transform.position - planet.position);

                flightAltitude = Vector3.Dot(planetToShipDirection, -1f * Vector3.forward);

                planetToShipDirection = planetToShipDirection.normalized;
                angularSpeed          = 360f * entryVelocity.magnitude / (2 * Mathf.PI * flightAltitude);
                axis = Vector3.Cross(planetToShipDirection, shipEntryHeading);

                orbitState = OrbitState.Entering;
            }
            else if (Input.GetButtonUp("Orbit") && !fc.interplanetaryFlight)
            {
                orbitState = OrbitState.Exiting;
            }

            if (!fc.interplanetaryFlight)
            {
                angularSpeed = Mathf.Lerp(angularSpeed, targetAngularSpeed, attackSpeed * Time.deltaTime);
                float angle = angularSpeed * Time.deltaTime;


                Quaternion rotation = Quaternion.Euler(angle * axis);

                Vector3 planetToShipDirection = (fc.transform.position - planet.position).normalized;
                Vector3 curDirection          = rotation * planetToShipDirection;
                tangent = Vector3.Cross(axis, planetToShipDirection);

                fc.transform.position = planet.transform.position + flightAltitude * curDirection;
                fc.transform.Rotate(axis, angle, Space.World);

                //Angular rotation
                angular += Input.GetAxis("Horizontal");
                fc.transform.rotation = Quaternion.LookRotation(tangent, planetToShipDirection);
                fc.transform.Rotate(planetToShipDirection, angular, Space.World);

                //Orbit axis rotation
                float axisMomentum = Input.GetAxis("Vertical") *
                                     Vector3.Dot(fc.transform.forward, tangent);


                if (orbitState == OrbitState.Entering)
                {
                    orbitState = OrbitState.Orbiting;
                }
                else if (orbitState == OrbitState.Exiting && InOrbitExitArea)
                {
                    fc.LeaveOrbit(angularSpeed * 2 * Mathf.PI * flightAltitude / 360f * shipEntryHeading);
                }
            }
        }
    }