コード例 #1
0
    public void FadeAudioSourceTo(AudioSource audioSource, float time, float fadeTo, Action callback = null)
    {
        float initialVolume = audioSource.volume;

        MHS.RunFor(time, t => {
            audioSource.volume = initialVolume + ((fadeTo - initialVolume) * t);
        }, () => {
            if (callback != null)
            {
                callback();
            }
        });
    }
コード例 #2
0
 public void FadeInAudioSource(AudioSource audioSource, Action callback = null)
 {
     if (audioSource != null)
     {
         audioSource.volume = 0;
         MHS.RunFor(2, t => {
             audioSource.volume = t;
         }, () => {
             audioSource.volume = 1;
             if (callback != null)
             {
                 callback();
             }
         });
     }
 }
コード例 #3
0
    // the Update loop contains a very simple example of moving the character around and controlling the animation
    void Update()
    {
        if (movementMode == MovementModes.None)
        {
            return;
        }

        // Crawling

        /*
         *      hit raycast looks above to see if anything above, just incase forced crouching is nessesary
         *      if not crawling + is grounded, and either the hit raycast collides, or if the movement mode is not no interaction and the player presses down, is grounded and is not pressing left or right
         *      Just added, check if anything below.. this helps with slopes... but may need some tweeking
         *      then begin crouching
         */
        RaycastHit2D hit     = Physics2D.Raycast(transform.position, Vector2.up, 1, LayerMask.GetMask("Default"), 0, 0);
        RaycastHit2D hitDown = Physics2D.Raycast(transform.position, Vector2.down, 0.6f, LayerMask.GetMask("Default"), 0, 0);

        if (movementMode != MovementModes.Crawling && _controller.isGrounded && hitDown.collider != null)
        {
            if ((hit.collider != null) || (movementMode != MovementModes.NoPlayerInteraction && _controller.isGrounded && Input.GetAxisRaw("Horizontal") == 0 && Input.GetAxisRaw("Vertical") == -1))
            {
                movementMode     = MovementModes.NoPlayerInteraction;
                startedCrouching = true;

                // Instant if forced
                if (hit.collider == null)
                {
                    animator.ResetTrigger("LayDown");
                    animator.SetTrigger("LayDown");
                }
                else
                {
                    animator.ResetTrigger("LayDownInstant");
                    animator.SetTrigger("LayDownInstant");
                }

                MHS.WaitRun(0.45f, () => {
                    movementMode     = MovementModes.Crawling;
                    startedCrouching = false;
                });
            }
        }

        if (movementMode == MovementModes.Normal || movementMode == MovementModes.NoPlayerInteraction)
        {
            if (_controller.isGrounded)
            {
                _velocity.y = 0;
            }

            if (movementMode != MovementModes.NoPlayerInteraction && Input.GetAxisRaw("Horizontal") == 1)
            {
                normalizedHorizontalSpeed = 1;
                directionFacing           = 1;

                //if( _controller.isGrounded )
                //	_animator.Play( Animator.StringToHash( "Run" ) );
            }
            else if (movementMode != MovementModes.NoPlayerInteraction && Input.GetAxisRaw("Horizontal") == -1)
            {
                directionFacing           = -1;
                normalizedHorizontalSpeed = -1;

                //if( _controller.isGrounded )
                //	_animator.Play( Animator.StringToHash( "Run" ) );
            }
            else
            {
                normalizedHorizontalSpeed = 0;

                //if( _controller.isGrounded )
                //	_animator.Play( Animator.StringToHash( "Idle" ) );
            }


            // we can only jump whilst grounded
            if (movementMode != MovementModes.NoPlayerInteraction && _controller.isGrounded && Input.GetButtonDown("Jump"))
            {
                AudioManager.instance.PlaySFX("Jump");
                _velocity.y = Mathf.Sqrt(jumpHeight * -gravity);

                MHS.RunFor(0.2f, i => {
                    if (Input.GetButton("Jump"))
                    {
                        _velocity.y = Mathf.Sqrt(jumpHeight * -gravity);
                    }
                });
            }


            // apply horizontal speed smoothing it. dont really do this with Lerp. Use SmoothDamp or something that provides more control
            var smoothedMovementFactor = _controller.isGrounded ? groundDamping : inAirDamping;             // how fast do we change direction?
            _velocity.x = Mathf.Lerp(_velocity.x, normalizedHorizontalSpeed * runSpeed, Time.deltaTime * smoothedMovementFactor);

            // apply gravity before moving
            _velocity.y += gravity * Time.deltaTime;

            // if holding down bump up our movement amount and turn off one way platform detection for a frame.
            // this lets us jump down through one way platforms
            if (_controller.isGrounded && Input.GetKey(KeyCode.DownArrow))
            {
                _velocity.y *= 3f;
                _controller.ignoreOneWayPlatformsThisFrame = true;
            }

            // Do the moving
            _controller.move(_velocity * Time.deltaTime);
        }
        else if (movementMode == MovementModes.SwimmingSurface)
        {
            float yPosOffset = -0.05f;
            if (enteringWater)
            {
                if (!startSurfaceing)
                {
                    _controller.move(_velocity * Time.deltaTime);
                }
                else
                {
                    _velocity = new Vector3(_velocity.x, _velocity.y += 20 * Time.deltaTime, _velocity.z);
                    _controller.move(_velocity * Time.deltaTime);
                }

                if (transform.position.y < topOfWater - 0.3f)
                {
                    startSurfaceing = true;
                }

                if (startSurfaceing && transform.position.y >= topOfWater + yPosOffset)
                {
                    startSurfaceing    = false;
                    paddleingRotation  = 0;
                    enteringWater      = false;
                    _velocity          = new Vector3(_velocity.x, 0, _velocity.z);
                    transform.position = new Vector3(transform.position.x, topOfWater + yPosOffset, transform.position.z);
                }
            }

            if (!enteringWater)
            {
                transform.position = new Vector3(transform.position.x, topOfWater + yPosOffset, transform.position.z);

                float rotateSpeed = 240;
                if (movementMode != MovementModes.NoPlayerInteraction && Input.GetAxisRaw("Horizontal") == 1)
                {
                    if (paddleingRotation > 0)
                    {
                        paddleingRotation /= 4;
                    }
                    paddleingRotation -= Time.deltaTime * rotateSpeed;

                    directionFacing           = 1;
                    normalizedHorizontalSpeed = 1;
                }
                else if (movementMode != MovementModes.NoPlayerInteraction && Input.GetAxisRaw("Horizontal") == -1)
                {
                    if (paddleingRotation < 0)
                    {
                        paddleingRotation /= 4;
                    }
                    paddleingRotation += Time.deltaTime * rotateSpeed;

                    directionFacing           = -1;
                    normalizedHorizontalSpeed = -1;
                }
                else
                {
                    if (paddleingRotation < Time.deltaTime * rotateSpeed)
                    {
                        paddleingRotation += Time.deltaTime * rotateSpeed;
                    }
                    if (paddleingRotation > Time.deltaTime * rotateSpeed)
                    {
                        paddleingRotation -= Time.deltaTime * rotateSpeed;
                    }

                    normalizedHorizontalSpeed = 0;
                }

                paddleingRotation  = Mathf.Clamp(paddleingRotation, -45f, 45f);
                transform.rotation = Quaternion.Euler(0, 0, paddleingRotation);

                var smoothedMovementFactor = _controller.isGrounded ? groundDamping : inAirDamping;                 // how fast do we change direction?
                _velocity.x = Mathf.Lerp(_velocity.x, normalizedHorizontalSpeed * runSpeed / 1.5f, Time.deltaTime * smoothedMovementFactor);
                _velocity.y = 0;

                if (Input.GetButtonDown("Jump"))
                {
                    MugHeadStudios.MHS.RunFor(0.1f, i => {
                        transform.rotation = Quaternion.Euler(0, 0, paddleingRotation * (1 - i));
                    }, () => {
                        transform.rotation = Quaternion.identity;
                    });
                    _velocity.y  = Mathf.Sqrt(2f * jumpHeight * -gravity);
                    movementMode = MovementModes.Normal;
                    Splash();
                }

                _controller.move(_velocity * Time.deltaTime);
            }
        }
        else if (movementMode == MovementModes.Crawling)
        {
            _velocity.y -= 15 * Time.deltaTime;

            if (Input.GetAxisRaw("Horizontal") == 1)
            {
                directionFacing = 1;
                _velocity.x     = 2;
            }
            else if (Input.GetAxisRaw("Horizontal") == -1)
            {
                directionFacing = -1;
                _velocity.x     = -2;
            }
            else
            {
                _velocity.x = 0;
            }

            _controller.move(_velocity * Time.deltaTime);

            hitDown = Physics2D.Raycast(transform.position, Vector2.down, 0.6f, LayerMask.GetMask("Default"), 0, 0);

            if (Input.GetAxisRaw("Vertical") != -1 || hitDown.collider == null)
            {
                // Coming up from crouch, check above head first
                hit = Physics2D.Raycast(transform.position, Vector2.up, 1, LayerMask.GetMask("Default"), 0, 0);
                if (hit.collider == null)
                {
                    movementMode = MovementModes.NoPlayerInteraction;
                    MHS.WaitRun(0.4f, () => {
                        movementMode = MovementModes.Normal;
                    });
                }
            }
        }

        // grab our current _velocity to use as a base for all calculations
        _velocity = _controller.velocity;

        // Animation
        animator.SetFloat("Running", (_controller.isGrounded && movementMode == MovementModes.Normal && Input.GetAxisRaw("Horizontal") != 0) ? 1 : 0);
        animator.SetBool("IsGrounded", _controller.isGrounded);
        animator.SetBool("IsPaddleing", (movementMode == MovementModes.SwimmingSurface));
        animator.SetBool("IsCrawling", (startedCrouching || movementMode == MovementModes.Crawling));
        animator.SetBool("CrawlingMoving", (movementMode == MovementModes.Crawling && Mathf.Abs(_velocity.x) > 0));
        animator.SetBool("PaddleingSwimming", (movementMode == MovementModes.SwimmingSurface && Input.GetAxisRaw("Horizontal") != 0));

        // Direction
        if (directionFacing == 1)
        {
            transform.localScale = new Vector3(1, transform.localScale.y, transform.localScale.z);
        }
        else if (directionFacing == -1)
        {
            transform.localScale = new Vector3(-1, transform.localScale.y, transform.localScale.z);
        }
    }