Esempio n. 1
0
        public override void ApplyMotion(CharacterViewModel characterViewModel)
        {
            var characterRigidbody = characterViewModel.GetComponent <Rigidbody>();
            var playerInput        = Input.GetAxis(AxisName);

            var animator = characterViewModel.GetComponent <Animator>();
            var currentAnimationSpeed = animator.GetFloat(AnimationSpeedVariable);

            float currentVelocity   = characterRigidbody.velocity.x;
            float newVelocity       = 0;
            float newAnimationSpeed = 0;

            if (InputInRange(playerInput))
            {
                newAnimationSpeed = Mathf.Clamp01(currentAnimationSpeed + Time.deltaTime * Acceleration);
                newVelocity       = currentVelocity + playerInput * SpeedMultiplier * Time.deltaTime * Acceleration;
            }
            else
            {
                newAnimationSpeed = Mathf.Clamp01(currentAnimationSpeed - Time.deltaTime * Deceleration);
                var interpolationFactor = Mathf.Clamp01(SpeedMultiplier * Time.deltaTime * Deceleration);
                newVelocity = Mathf.Lerp(currentVelocity, 0f, interpolationFactor);
            }

            if (SpeedInRange(newVelocity))
            {
                UpdateHorizontalVelocity(characterRigidbody, newVelocity);
            }
            animator.SetFloat(AnimationSpeedVariable, newAnimationSpeed);
        }
Esempio n. 2
0
        public override void ApplyMotion(CharacterViewModel characterViewModel)
        {
            var characterRigidbody = characterViewModel.GetComponent <Rigidbody>();

            if (HorizontalVelocity(characterRigidbody) < SpeedThreshold)
            {
                float   axisValue   = Input.GetAxis(AxisName);
                Vector3 forceVector = (RunningForce * axisValue) * Vector3.right;
                characterRigidbody.AddForce(forceVector, ForceMode.Impulse);
            }
        }
    public override void ApplyMotion(CharacterViewModel characterViewModel)
    {
        var    characterTransform = characterViewModel.transform;
        var    animator           = characterViewModel.GetComponent <Animator>();
        Camera mainCamera         = GameObject.Find(MainCameraName).GetComponentInChildren <Camera>();

        bool facingRight = animator.GetBool(FacingRightAnimatorParameter);

        var headTransform = animator.GetBoneTransform(HumanBodyBones.Head);
        var headPosition  = mainCamera.WorldToScreenPoint(headTransform.position);
        var headDirection = mainCamera.WorldToScreenPoint(headPosition - Input.mousePosition);
        var headAngle     = Vector3.Angle(headDirection, (-1) * characterTransform.up);

        if (Input.mousePosition.x - 20.0f >= headPosition.x)
        {
            animator.SetFloat(HeadTiltAnimatiorParameter, (headAngle - 20.0f) / 180.0f);
        }

        if (Input.mousePosition.x + 20.0f <= headPosition.x)
        {
            animator.SetFloat(HeadTiltAnimatiorParameter, (200.0f - headAngle) / 180.0f);
        }

        if (Input.mousePosition.x - 50.0f >= headPosition.x && facingRight == false)
        {
            animator.SetBool(FacingRightAnimatorParameter, true);
        }

        if (Input.mousePosition.x + 50.0f <= headPosition.x && facingRight == true)
        {
            animator.SetBool(FacingRightAnimatorParameter, false);
        }

        float y, targetY, remainingY;

        y = targetY = remainingY = 0f;
        if (!facingRight)
        {
            targetY = 180.0f;
        }
        if (y != targetY && remainingY == 0)
        {
            remainingY += 180.0f;
        }

        if (remainingY > 0)
        {
            if (remainingY <= Time.deltaTime * RotationSpeed)
            {
                if (y < 180.0f)
                {
                    y = 180.0f;
                }
                else
                {
                    y = 0.0f;
                }
                remainingY = 0.0f;

                if (y >= 360.0f)
                {
                    y -= 360.0f;
                }
            }
            else if (y != targetY)
            {
                y          += Time.deltaTime * RotationSpeed;
                remainingY -= Time.deltaTime * RotationSpeed;

                if (y >= 360.0f)
                {
                    y -= 360.0f;
                }
            }
        }
        characterTransform.localRotation = Quaternion.Euler(0, y + RotationOffset, 0);
    }