CharacterMotorC.UpdateFunction C# (CSharp) Method

UpdateFunction() public method

public UpdateFunction ( ) : void
return void
    void UpdateFunction()
    {
        // We copy the actual velocity into a temporary variable that we can manipulate.
        Vector3 velocity = movement.velocity;
        // Update velocity based on input
        velocity = ApplyInputVelocityChange(velocity);
        // Apply gravity and jumping force
        velocity = ApplyGravityAndJumping (velocity);
        // Moving platform support
        Vector3 moveDistance = Vector3.zero;

        if (MoveWithPlatform()) {
            Vector3 newGlobalPoint = movingPlatform.activePlatform.TransformPoint(movingPlatform.activeLocalPoint);
            moveDistance = (newGlobalPoint - movingPlatform.activeGlobalPoint);
            if (moveDistance != Vector3.zero)
                controller.Move(moveDistance);
            // Support moving platform rotation as well:
            Quaternion newGlobalRotation = movingPlatform.activePlatform.rotation * movingPlatform.activeLocalRotation;
            Quaternion rotationDiff = newGlobalRotation * Quaternion.Inverse(movingPlatform.activeGlobalRotation);
            float yRotation = rotationDiff.eulerAngles.y;
            if (yRotation != 0) {
                // Prevent rotation of the local up vector
                tr.Rotate(0, yRotation, 0);
            }
        }
        // Save lastPosition for velocity calculation.
        Vector3 lastPosition = tr.position;
        // We always want the movement to be framerate independent.  Multiplying by Time.deltaTime does this.
        Vector3 currentMovementOffset = velocity * Time.deltaTime;
        // Find out how much we need to push towards the ground to avoid loosing grouning
        // when walking down a step or over a sharp change in slope.
        float mathf1 = controller.stepOffset;
        float mathf2 = new Vector3(currentMovementOffset.x, 0, currentMovementOffset.z).magnitude;
        float pushDownOffset = Mathf.Max(mathf1, mathf2);
        if (grounded)
            currentMovementOffset -= pushDownOffset * Vector3.up;
        // Reset variables that will be set by collision function
        movingPlatform.hitPlatform = null;
        groundNormal = Vector3.zero;
        // Move our character!
        movement.collisionFlags = controller.Move (currentMovementOffset);
        movement.lastHitPoint = movement.hitPoint;
        lastGroundNormal = groundNormal;
        if (movingPlatform.enabled && movingPlatform.activePlatform != movingPlatform.hitPlatform) {
            if (movingPlatform.hitPlatform != null) {
                movingPlatform.activePlatform = movingPlatform.hitPlatform;
                movingPlatform.lastMatrix = movingPlatform.hitPlatform.localToWorldMatrix;
                movingPlatform.newPlatform = true;
            }
        }
        // Calculate the velocity based on the current and previous position.
        // This means our velocity will only be the amount the character actually moved as a result of collisions.
        Vector3 oldHVelocity = new Vector3(velocity.x, 0, velocity.z);
        movement.velocity = (tr.position - lastPosition) / Time.deltaTime;
        Vector3 newHVelocity = new Vector3(movement.velocity.x, 0, movement.velocity.z);
        // The CharacterController can be moved in unwanted directions when colliding with things.
        // We want to prevent this from influencing the recorded velocity.
        if (oldHVelocity == Vector3.zero) {
            movement.velocity = new Vector3(0, movement.velocity.y, 0);
        }
        else {
            float projectedNewVelocity = Vector3.Dot(newHVelocity, oldHVelocity) / oldHVelocity.sqrMagnitude;
            movement.velocity = oldHVelocity * Mathf.Clamp01(projectedNewVelocity) + movement.velocity.y * Vector3.up;
        }
        if (movement.velocity.y < velocity.y - 0.001) {
            if (movement.velocity.y < 0) {
                // Something is forcing the CharacterController down faster than it should.
                // Ignore this
                movement.velocity.y = velocity.y;
            }
            else {
                // The upwards movement of the CharacterController has been blocked.
                // This is treated like a ceiling collision - stop further jumping here.
                jumping.holdingJumpButton = false;
            }
        }
        // We were grounded but just loosed grounding
        if (grounded && !IsGroundedTest()) {
            grounded = false;
            // Apply inertia from platform
            if (movingPlatform.enabled &&
                (movingPlatform.movementTransfer == MovementTransferOnJump.InitTransfer ||
                 movingPlatform.movementTransfer == MovementTransferOnJump.PermaTransfer)) {
                movement.frameVelocity = movingPlatform.platformVelocity;
                movement.velocity += movingPlatform.platformVelocity;
            }
            SendMessage("OnFall", SendMessageOptions.DontRequireReceiver);
            // We pushed the character down to ensure it would stay on the ground if there was any.
            // But there wasn't so now we cancel the downwards offset to make the fall smoother.
            tr.position += pushDownOffset * Vector3.up;
        }
        // We were not grounded but just landed on something
        else if (!grounded && IsGroundedTest()) {
            grounded = true;
            jumping.jumping = false;
            SubtractNewPlatformVelocity();
            SendMessage("OnLand", SendMessageOptions.DontRequireReceiver);
        }
        // Moving platforms support
        if (MoveWithPlatform()) {
            // Use the center of the lower half sphere of the capsule as reference point.
            // This works best when the character is standing on moving tilting platforms.
            //movingPlatform.activeGlobalPoint = tr.position +
            //float thisstuff = (float)(controller.center.y - controller.height*0.5 + controller.radius);
            //Vector3 testthis = Vector3.up * thisstuff;
            //movingPlatform.activeGlobalPoint *= (controller.center.y - controller.height*0.5 + controller.radius);
            movingPlatform.activeLocalPoint = movingPlatform.activePlatform.InverseTransformPoint(movingPlatform.activeGlobalPoint);
            // Support moving platform rotation as well:
            movingPlatform.activeGlobalRotation = tr.rotation;
            movingPlatform.activeLocalRotation = Quaternion.Inverse(movingPlatform.activePlatform.rotation) * movingPlatform.activeGlobalRotation;
        }
    }