コード例 #1
0
    void Update()
    {
        bool dead = entity.dead;

        if (dead == true)
        {
            resetPlayer();
            return;
        }

        //reset acceleration upon collision
        if (playerPhysics.movementStopped)
        {
            targetSpeed  = 0;
            currentSpeed = 0;
        }

        //if player is touching ground
        if (playerPhysics.grounded)
        {
            amountToMove.y = 0;

            if (jumping)
            {
                jumping           = false;
                timesDoubleJumped = 0;
                //animator.SetBool("Jumping",false);
            }

            if (sliding)
            {
                if (Mathf.Abs(currentSpeed) < .25f)
                {
                    sliding = false;
                    //animator.SetBool("Sliding",false);

                    playerPhysics.ResetCollider();
                }
            }

            //jump
            if (Input.GetButtonDown("Jump"))
            {
                amountToMove.y = jumpHeight;
                jumping        = true;

                sprite.Jump();
                //animator.SetBool("Jumping",true);
            }
            else
            {
                sprite.Reset();
            }

            //sliding
            if (currentSpeed != 0 && Input.GetButtonDown("Slide"))
            {
                sliding = true;
                //animator.SetBool("Sliding",true);
                targetSpeed = 0;

                playerPhysics.SetCollider(new Vector3(1f, 0.5f, 1f), new Vector3(0, -0.25f, 0));
            }
        }
        else if (timesDoubleJumped < doubleJumps)
        {
            if (jumping &&
                Input.GetButtonDown("Jump") &&
                lastVelocity.y < doubleJumpStartVelocity &&
                lastVelocity.y > doubleJumpEndVelocity)
            {
                amountToMove.y = doubleJumpHeight;
                timesDoubleJumped++;

                sprite.DoubleJump();
            }
        }

        //animationSpeed = IncrementTowards (animationSpeed,Mathf.Abs (targetSpeed),accleration);
        //animator.SetFloat ("Speed",animationSpeed);

        //Input
        if (!sliding)
        {
            float speed = (Input.GetButton("Run"))?runspeed:walkspeed;
            targetSpeed  = Input.GetAxisRaw("Horizontal") * speed;
            currentSpeed = IncrementTowards(currentSpeed, targetSpeed, acceleration);

            //face direction
            float moveDir = Input.GetAxisRaw("Horizontal");
            if (moveDir != 0)
            {
                //transform.eulerAngles = (moveDir>0)?Vector3.up * 180:Vector3.zero;
                sprite.sprite.mirror = moveDir < 0;
            }
        }
        else
        {
            currentSpeed = IncrementTowards(currentSpeed, targetSpeed, slideDeceleration);
        }

        //Amount to Move
        amountToMove.x  = currentSpeed;
        amountToMove.y -= gravity * Time.deltaTime;

        /*if (currentSpeed ==0)
         *      playerPhysics.SetCollider (new Vector3(0.5f,1f,1f), Vector3.zero);
         * else
         *      playerPhysics.ResetCollider ();*/

        Vector3 currentPosition = transform.position;

        playerPhysics.Move(amountToMove * Time.deltaTime);
        lastVelocity = (transform.position - currentPosition) / Time.deltaTime;
    }