private Vector3 ApplyGravityAndJumping(Vector3 velocity)
 {
     if (!this.inputJump || !this.canControl)
     {
         this.jumping.holdingJumpButton  = false;
         this.jumping.lastButtonDownTime = -100;
     }
     if ((this.inputJump && (this.jumping.lastButtonDownTime < 0)) && this.canControl)
     {
         this.jumping.lastButtonDownTime = Time.time;
     }
     if (this.grounded)
     {
         velocity.y = Mathf.Min(0, velocity.y) - (this.movement.gravity * Time.deltaTime);
     }
     else
     {
         velocity.y = this.movement.velocity.y - (this.movement.gravity * Time.deltaTime);
         if (this.jumping.jumping && this.jumping.holdingJumpButton)
         {
             if (Time.time < (this.jumping.lastStartTime + (this.jumping.extraHeight / this.CalculateJumpVerticalSpeed(this.jumping.baseHeight))))
             {
                 velocity = velocity + ((this.jumping.jumpDir * this.movement.gravity) * Time.deltaTime);
             }
         }
         velocity.y = Mathf.Max(velocity.y, -this.movement.maxFallSpeed);
     }
     if (this.grounded)
     {
         if ((this.jumping.enabled && this.canControl) && ((Time.time - this.jumping.lastButtonDownTime) < 0.2f))
         {
             this.grounded                   = false;
             this.jumping.jumping            = true;
             this.jumping.lastStartTime      = Time.time;
             this.jumping.lastButtonDownTime = -100;
             this.jumping.holdingJumpButton  = true;
             if (this.TooSteep())
             {
                 this.jumping.jumpDir = Vector3.Slerp(Vector3.up, this.groundNormal, this.jumping.steepPerpAmount);
             }
             else
             {
                 this.jumping.jumpDir = Vector3.Slerp(Vector3.up, this.groundNormal, this.jumping.perpAmount);
             }
             velocity.y = 0;
             velocity   = velocity + (this.jumping.jumpDir * this.CalculateJumpVerticalSpeed(this.jumping.baseHeight));
             if (this.movingPlatform.enabled && ((this.movingPlatform.movementTransfer == MovementTransferOnJump.InitTransfer) || (this.movingPlatform.movementTransfer == MovementTransferOnJump.PermaTransfer)))
             {
                 this.movement.frameVelocity = this.movingPlatform.platformVelocity;
                 velocity = velocity + this.movingPlatform.platformVelocity;
             }
             this.SendMessage("OnJump", SendMessageOptions.DontRequireReceiver);
         }
         else
         {
             this.jumping.holdingJumpButton = false;
         }
     }
     return(velocity);
 }
예제 #2
0
 private Vector3 ApplyGravityAndJumping(Vector3 velocity)
 {
     if (!this.inputJump || !this.canControl)
     {
         this.jumping.holdingJumpButton  = false;
         this.jumping.lastButtonDownTime = (float)-100;
     }
     if (this.inputJump && this.jumping.lastButtonDownTime < (float)0 && this.canControl)
     {
         this.jumping.lastButtonDownTime = Time.get_time();
     }
     if (this.grounded)
     {
         velocity.y = Mathf.Min((float)0, velocity.y) - this.movement.gravity * Time.get_deltaTime();
     }
     else
     {
         velocity.y = this.movement.velocity.y - this.movement.gravity * Time.get_deltaTime();
         if (this.jumping.jumping && this.jumping.holdingJumpButton && Time.get_time() < this.jumping.lastStartTime + this.jumping.extraHeight / this.CalculateJumpVerticalSpeed(this.jumping.baseHeight))
         {
             velocity += this.jumping.jumpDir * this.movement.gravity * Time.get_deltaTime();
         }
         velocity.y = Mathf.Max(velocity.y, -this.movement.maxFallSpeed);
     }
     if (this.grounded)
     {
         if (this.jumping.enabled && this.canControl && Time.get_time() - this.jumping.lastButtonDownTime < 0.2f)
         {
             this.grounded                   = false;
             this.jumping.jumping            = true;
             this.jumping.lastStartTime      = Time.get_time();
             this.jumping.lastButtonDownTime = (float)-100;
             this.jumping.holdingJumpButton  = true;
             if (this.TooSteep())
             {
                 this.jumping.jumpDir = Vector3.Slerp(Vector3.get_up(), this.groundNormal, this.jumping.steepPerpAmount);
             }
             else
             {
                 this.jumping.jumpDir = Vector3.Slerp(Vector3.get_up(), this.groundNormal, this.jumping.perpAmount);
             }
             velocity.y = (float)0;
             velocity  += this.jumping.jumpDir * this.CalculateJumpVerticalSpeed(this.jumping.baseHeight);
             if (this.movingPlatform.enabled && (this.movingPlatform.movementTransfer == MovementTransferOnJump.InitTransfer || this.movingPlatform.movementTransfer == MovementTransferOnJump.PermaTransfer))
             {
                 this.movement.frameVelocity = this.movingPlatform.platformVelocity;
                 velocity += this.movingPlatform.platformVelocity;
             }
             this.SendMessage("OnJump", 1);
         }
         else
         {
             this.jumping.holdingJumpButton = false;
         }
     }
     return(velocity);
 }
예제 #3
0
 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;
 }
 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;
 }
    private void UpdateFunction()
    {
        Vector3 velocity = this.movement.velocity;

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

        if (this.MoveWithPlatform())
        {
            Vector3 newGlobalPoint = this.movingPlatform.activePlatform.TransformPoint(this.movingPlatform.activeLocalPoint);
            moveDistance = newGlobalPoint - this.movingPlatform.activeGlobalPoint;
            if (moveDistance != Vector3.zero)
            {
                this.controller.Move(moveDistance);
            }
            Quaternion newGlobalRotation = this.movingPlatform.activePlatform.rotation * this.movingPlatform.activeLocalRotation;
            Quaternion rotationDiff      = newGlobalRotation * Quaternion.Inverse(this.movingPlatform.activeGlobalRotation);
            float      yRotation         = rotationDiff.eulerAngles.y;
            if (yRotation != 0)
            {
                this.tr.Rotate(0, yRotation, 0);
            }
        }
        Vector3 lastPosition          = this.tr.position;
        Vector3 currentMovementOffset = velocity * Time.deltaTime;
        float   pushDownOffset        = Mathf.Max(this.controller.stepOffset, new Vector3(currentMovementOffset.x, 0, currentMovementOffset.z).magnitude);

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

        this.movement.velocity = (this.tr.position - lastPosition) / Time.deltaTime;
        Vector3 newHVelocity = new Vector3(this.movement.velocity.x, 0, this.movement.velocity.z);

        if (oldHVelocity == Vector3.zero)
        {
            this.movement.velocity = new Vector3(0, this.movement.velocity.y, 0);
        }
        else
        {
            float projectedNewVelocity = Vector3.Dot(newHVelocity, oldHVelocity) / oldHVelocity.sqrMagnitude;
            this.movement.velocity = (oldHVelocity * Mathf.Clamp01(projectedNewVelocity)) + (this.movement.velocity.y * Vector3.up);
        }
        if (this.movement.velocity.y < (velocity.y - 0.001f))
        {
            if (this.movement.velocity.y < 0)
            {
                this.movement.velocity.y = velocity.y;
            }
            else
            {
                this.jumping.holdingJumpButton = false;
            }
        }
        if (this.grounded && !this.IsGroundedTest())
        {
            this.grounded = false;
            if (this.movingPlatform.enabled && ((this.movingPlatform.movementTransfer == MovementTransferOnJump.InitTransfer) || (this.movingPlatform.movementTransfer == MovementTransferOnJump.PermaTransfer)))
            {
                this.movement.frameVelocity = this.movingPlatform.platformVelocity;
                this.movement.velocity      = this.movement.velocity + this.movingPlatform.platformVelocity;
            }
            this.SendMessage("OnFall", SendMessageOptions.DontRequireReceiver);
            this.tr.position = this.tr.position + (pushDownOffset * Vector3.up);
        }
        else
        {
            if (!this.grounded && this.IsGroundedTest())
            {
                this.grounded        = true;
                this.jumping.jumping = false;
                this.StartCoroutine(this.SubtractNewPlatformVelocity());
                this.SendMessage("OnLand", SendMessageOptions.DontRequireReceiver);
            }
        }
        if (this.MoveWithPlatform())
        {
            this.movingPlatform.activeGlobalPoint    = this.tr.position + (Vector3.up * ((this.controller.center.y - (this.controller.height * 0.5f)) + this.controller.radius));
            this.movingPlatform.activeLocalPoint     = this.movingPlatform.activePlatform.InverseTransformPoint(this.movingPlatform.activeGlobalPoint);
            this.movingPlatform.activeGlobalRotation = this.tr.rotation;
            this.movingPlatform.activeLocalRotation  = Quaternion.Inverse(this.movingPlatform.activePlatform.rotation) * this.movingPlatform.activeGlobalRotation;
        }
    }
예제 #6
0
    private void UpdateFunction()
    {
        Vector3 vector = this.movement.velocity;

        vector = this.ApplyInputVelocityChange(vector);
        vector = this.ApplyGravityAndJumping(vector);
        Vector3 vector2 = Vector3.get_zero();

        if (this.MoveWithPlatform())
        {
            Vector3 vector3 = this.movingPlatform.activePlatform.TransformPoint(this.movingPlatform.activeLocalPoint);
            vector2 = vector3 - this.movingPlatform.activeGlobalPoint;
            if (vector2 != Vector3.get_zero())
            {
                this.controller.Move(vector2);
            }
            Quaternion quaternion = this.movingPlatform.activePlatform.get_rotation() * this.movingPlatform.activeLocalRotation;
            float      y          = (quaternion * Quaternion.Inverse(this.movingPlatform.activeGlobalRotation)).get_eulerAngles().y;
            if (y != (float)0)
            {
                this.tr.Rotate((float)0, y, (float)0);
            }
        }
        Vector3 position = this.tr.get_position();
        Vector3 vector4  = vector * Time.get_deltaTime();
        float   num      = Mathf.Max(this.controller.get_stepOffset(), new Vector3(vector4.x, (float)0, vector4.z).get_magnitude());

        if (this.grounded)
        {
            vector4 -= num * Vector3.get_up();
        }
        this.movingPlatform.hitPlatform = null;
        this.groundNormal            = Vector3.get_zero();
        this.movement.collisionFlags = this.controller.Move(vector4);
        this.movement.lastHitPoint   = this.movement.hitPoint;
        this.lastGroundNormal        = this.groundNormal;
        if (this.movingPlatform.enabled && this.movingPlatform.activePlatform != this.movingPlatform.hitPlatform && this.movingPlatform.hitPlatform != null)
        {
            this.movingPlatform.activePlatform = this.movingPlatform.hitPlatform;
            this.movingPlatform.lastMatrix     = this.movingPlatform.hitPlatform.get_localToWorldMatrix();
            this.movingPlatform.newPlatform    = true;
        }
        Vector3 vector5 = new Vector3(vector.x, (float)0, vector.z);

        this.movement.velocity = (this.tr.get_position() - position) / Time.get_deltaTime();
        Vector3 vector6 = new Vector3(this.movement.velocity.x, (float)0, this.movement.velocity.z);

        if (vector5 == Vector3.get_zero())
        {
            this.movement.velocity = new Vector3((float)0, this.movement.velocity.y, (float)0);
        }
        else
        {
            float num2 = Vector3.Dot(vector6, vector5) / vector5.get_sqrMagnitude();
            this.movement.velocity = vector5 * Mathf.Clamp01(num2) + this.movement.velocity.y * Vector3.get_up();
        }
        if (this.movement.velocity.y < vector.y - 0.001f)
        {
            if (this.movement.velocity.y < (float)0)
            {
                this.movement.velocity.y = vector.y;
            }
            else
            {
                this.jumping.holdingJumpButton = false;
            }
        }
        if (this.grounded && !this.IsGroundedTest())
        {
            this.grounded = false;
            if (this.movingPlatform.enabled && (this.movingPlatform.movementTransfer == MovementTransferOnJump.InitTransfer || this.movingPlatform.movementTransfer == MovementTransferOnJump.PermaTransfer))
            {
                this.movement.frameVelocity = this.movingPlatform.platformVelocity;
                this.movement.velocity      = this.movement.velocity + this.movingPlatform.platformVelocity;
            }
            this.SendMessage("OnFall", 1);
            this.tr.set_position(this.tr.get_position() + num * Vector3.get_up());
        }
        else if (!this.grounded && this.IsGroundedTest())
        {
            this.grounded        = true;
            this.jumping.jumping = false;
            this.StartCoroutine_Auto(this.SubtractNewPlatformVelocity());
            this.SendMessage("OnLand", 1);
        }
        if (this.MoveWithPlatform())
        {
            this.movingPlatform.activeGlobalPoint    = this.tr.get_position() + Vector3.get_up() * (this.controller.get_center().y - this.controller.get_height() * 0.5f + this.controller.get_radius());
            this.movingPlatform.activeLocalPoint     = this.movingPlatform.activePlatform.InverseTransformPoint(this.movingPlatform.activeGlobalPoint);
            this.movingPlatform.activeGlobalRotation = this.tr.get_rotation();
            this.movingPlatform.activeLocalRotation  = Quaternion.Inverse(this.movingPlatform.activePlatform.get_rotation()) * this.movingPlatform.activeGlobalRotation;
        }
    }