Fly.Update C# (CSharp) Method

Update() public method

public Update ( ) : void
return void
	void  Update () {  
	
		// Get the horizontal and vertical axis.
		// By default they are mapped to the arrow keys.
		// The value is in the range -1 to 1
        if (isEnabled)
        {
            float angularX = Input.GetAxis("AngularX") * rotationTorque * Time.deltaTime;
            float angularY = Input.GetAxis("AngularY") * rotationTorque * Time.deltaTime;
            float angularZ = -1 * Input.GetAxis("AngularZ") * rotationTorque * Time.deltaTime;
            float linearX = Input.GetAxis("LinearX") * translationalSpeed * Time.deltaTime;
            float linearY = Input.GetAxis("LinearY") * translationalSpeed * Time.deltaTime;
            float linearZ = Input.GetAxis("LinearZ") * translationalSpeed * Time.deltaTime;
            throttle = throttle + Input.GetAxis("Throttle") * Time.deltaTime * throttleSpeed;
            throttle = Mathf.Clamp(throttle, 0.0f, 10.0f);

            Vector3 relativeForward = transform.TransformDirection(Vector3.forward);
            Vector3 angularTorqueVector = new Vector3(angularX, angularY, angularZ);

            myRigidbody.AddForce(relativeForward * throttle * thrust / 10);
            myRigidbody.AddTorque(transform.rotation * angularTorqueVector);
            myRigidbody.AddForce(linearX, linearY, linearZ); //Space.World = Translate in world space - local space is default

            transform.position += new Vector3(linearX, linearY, linearZ);

            earthDistance = (transform.position - earth.position).magnitude;
            earthAltitude = earthDistance - earthRadius;
            atmosphericDensity = Mathf.Exp(-earthAltitude / scaleHeight);

            if (sas == true && angularX == 0.0f && angularY == 0.0f && angularZ == 0.0f)
            {

                myRigidbody.AddTorque(-sasForce * myRigidbody.angularVelocity, ForceMode.Force);
            }
            foreach (ParticleSystem child in engineList)
            {
                if (throttle != 0.0)
                {
                    child.GetComponent<ParticleSystem>().enableEmission = true;
                    child.GetComponent<ParticleSystem>().startLifetime = Mathf.Lerp(engineLifetimeBounds.x, engineLifetimeBounds.y, throttle / 10.0f);
                    child.GetComponent<ParticleSystem>().startSize = Mathf.Lerp(engineSizeBounds.x, engineSizeBounds.y, throttle / 10.0f);
                }
                else
                {
                    child.GetComponent<ParticleSystem>().enableEmission = false;
                }
            }
            foreach (ParticleSystem child in exhaustList)
            {
                if (throttle != 0.0)
                {
                    child.GetComponent<ParticleSystem>().enableEmission = true;
                    child.GetComponent<ParticleSystem>().startLifetime = Mathf.Lerp(exhaustLifetimeBounds.x, exhaustLifetimeBounds.y, throttle / 10.0f);
                    child.GetComponent<ParticleSystem>().startSize = Mathf.Lerp(exhaustSizeBounds.x, exhaustSizeBounds.y, throttle / 10.0f);
                    child.GetComponent<ParticleSystem>().emissionRate = Mathf.Lerp(exhaustEmissionBounds.x, exhaustEmissionBounds.y, atmosphericDensity);
                }
                else
                {
                    child.GetComponent<ParticleSystem>().enableEmission = false;
                }
            }
            foreach (AudioSource child in audioSourceList)
            {
                child.GetComponent<AudioSource>().volume = throttle / 10.0f * atmosphericDensity;
            }
            if (Input.GetKeyDown(KeyCode.T))
            {
                sas = !sas;
            }
            if (Input.GetKeyDown(KeyCode.F))
            {
                sas = !sas;
            }
            if (Input.GetKeyUp(KeyCode.F))
            {
                sas = !sas;
            }
            if (Input.GetKeyUp(KeyCode.G))
            {
                gearDown = !gearDown;
                if (animator)
                {
                    animator.SetBool("GearDown", gearDown);
                }
            }
        }
        else
        {
            transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * 5.0f);
            transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 5.0f);
        }
	}

Usage Example

Ejemplo n.º 1
0
        public static void Main()
        {
            //create a miner
            Miner bob = new Miner(0);

            //create his wife
            MinersWife elsa = new MinersWife(1);

            // create Bar's Fly
            Fly fly = new Fly(2);


            //register them with the entity manager
            EntityManager.Instance.RegisterEntity(bob);
            EntityManager.Instance.RegisterEntity(elsa);
            EntityManager.Instance.RegisterEntity(fly);

            //run Bob and Elsa through a few Update calls
            for (int i = 0; i < 50; i++)
            {
                bob.Update();
                elsa.Update();
                fly.Update();
                MessageDispatcher.Instance.DispatchDelayedMessages();
                System.Threading.Thread.Sleep(500);
            }
        }