public CharacterMotor()
 {
     this.canControl         = true;
     this.useFixedUpdate     = true;
     this.inputMoveDirection = Vector3.zero;
     this.movement           = new CharacterMotorMovement();
     this.jumping            = new CharacterMotorJumping();
     this.movingPlatform     = new CharacterMotorMovingPlatform();
     this.sliding            = new CharacterMotorSliding();
     this.grounded           = true;
     this.groundNormal       = Vector3.zero;
     this.lastGroundNormal   = Vector3.zero;
 }
 public CharacterMotor()
 {
     canControl         = true;
     useFixedUpdate     = true;
     inputMoveDirection = Vector3.zero;
     movement           = new CharacterMotorMovement();
     jumping            = new CharacterMotorJumping();
     movingPlatform     = new CharacterMotorMovingPlatform();
     sliding            = new CharacterMotorSliding();
     grounded           = true;
     groundNormal       = Vector3.zero;
     lastGroundNormal   = Vector3.zero;
 }
Example #3
0
    private void UpdateFunction()
    {
        Vector3 velocity = movement.velocity;

        velocity = ApplyInputVelocityChange(velocity);
        velocity = ApplyGravityAndJumping(velocity);
        Vector3 zero = Vector3.zero;

        if (MoveWithPlatform())
        {
            Vector3 a = movingPlatform.activePlatform.TransformPoint(movingPlatform.activeLocalPoint);
            zero = a - movingPlatform.activeGlobalPoint;
            if (zero != Vector3.zero)
            {
                controller.Move(zero);
            }
            Quaternion lhs         = movingPlatform.activePlatform.rotation * movingPlatform.activeLocalRotation;
            Vector3    eulerAngles = (lhs * Quaternion.Inverse(movingPlatform.activeGlobalRotation)).eulerAngles;
            float      y           = eulerAngles.y;
            if (y != 0f)
            {
                tr.Rotate(0f, y, 0f);
            }
        }
        Vector3 position = tr.position;
        Vector3 motion   = velocity * Time.deltaTime;
        float   d        = Mathf.Max(controller.stepOffset, new Vector3(motion.x, 0f, motion.z).magnitude);

        if (grounded)
        {
            motion -= d * Vector3.up;
        }
        movingPlatform.hitPlatform = null;
        groundNormal            = Vector3.zero;
        movement.collisionFlags = controller.Move(motion);
        movement.lastHitPoint   = movement.hitPoint;
        lastGroundNormal        = groundNormal;
        if (movingPlatform.enabled && movingPlatform.activePlatform != movingPlatform.hitPlatform && movingPlatform.hitPlatform != null)
        {
            movingPlatform.activePlatform = movingPlatform.hitPlatform;
            movingPlatform.lastMatrix     = movingPlatform.hitPlatform.localToWorldMatrix;
            movingPlatform.newPlatform    = true;
        }
        Vector3 vector = new Vector3(velocity.x, 0f, velocity.z);

        movement.velocity = (tr.position - position) / Time.deltaTime;
        Vector3 lhs2 = new Vector3(movement.velocity.x, 0f, movement.velocity.z);

        if (vector == Vector3.zero)
        {
            movement.velocity = new Vector3(0f, movement.velocity.y, 0f);
        }
        else
        {
            float value = Vector3.Dot(lhs2, vector) / vector.sqrMagnitude;
            movement.velocity = vector * Mathf.Clamp01(value) + movement.velocity.y * Vector3.up;
        }
        if (!(movement.velocity.y >= velocity.y - 0.001f))
        {
            if (!(movement.velocity.y >= 0f))
            {
                movement.velocity.y = velocity.y;
            }
            else
            {
                jumping.holdingJumpButton = false;
            }
        }
        if (grounded && !IsGroundedTest())
        {
            grounded = false;
            if (movingPlatform.enabled && (movingPlatform.movementTransfer == MovementTransferOnJump.InitTransfer || movingPlatform.movementTransfer == MovementTransferOnJump.PermaTransfer))
            {
                movement.frameVelocity = movingPlatform.platformVelocity;
                movement.velocity     += movingPlatform.platformVelocity;
            }
            SendMessage("OnFall", SendMessageOptions.DontRequireReceiver);
            tr.position += d * Vector3.up;
        }
        else if (!grounded && IsGroundedTest())
        {
            grounded        = true;
            jumping.jumping = false;
            StartCoroutine(SubtractNewPlatformVelocity());
            SendMessage("OnLand", SendMessageOptions.DontRequireReceiver);
        }
        if (MoveWithPlatform())
        {
            CharacterMotorMovingPlatform characterMotorMovingPlatform = movingPlatform;
            Vector3 position2 = tr.position;
            Vector3 up        = Vector3.up;
            Vector3 center    = controller.center;
            characterMotorMovingPlatform.activeGlobalPoint = position2 + up * (center.y - controller.height * 0.5f + controller.radius);
            movingPlatform.activeLocalPoint     = movingPlatform.activePlatform.InverseTransformPoint(movingPlatform.activeGlobalPoint);
            movingPlatform.activeGlobalRotation = tr.rotation;
            movingPlatform.activeLocalRotation  = Quaternion.Inverse(movingPlatform.activePlatform.rotation) * movingPlatform.activeGlobalRotation;
        }
    }