Ejemplo n.º 1
0
    private void FixedUpdate()
    {
        // update x
        float x = Input.GetAxis("Horizontal");

        velocity.x = x * _speed;

        // change direction
        if (x < 0)
        {
            GetComponent <SpriteRenderer>().flipX = false;
        }
        else
        {
            GetComponent <SpriteRenderer>().flipX = true;
        }

        // jump
        if (Input.GetAxis("Jump") != 0 && td.IsTouching())
        {
            rb.AddForce(new Vector2(0, _jump_pulse), ForceMode2D.Impulse);
            fc.addToSugar(lostOnJump);
        }

        // update position
        velocity            = transform.TransformDirection(velocity);
        transform.position += velocity * Time.deltaTime;

        // update animation
        animator.SetFloat("speed", Mathf.Abs(x));
        animator.SetBool("isJumping", !td.IsTouching());
        animator.SetFloat("high", transform.position.y); //check if falling

        // update sugar
        if (x != 0)
        {
            fc.addToSugar(lostOnWalk);
        }
    }
 /*
  * Note that updates related to the physics engine should be done in FixedUpdate and not in Update!
  */
 private void FixedUpdate()
 {
     if (td.IsTouching())    // allow to walk and jump
     {
         float horizontal = Input.GetAxis("Horizontal");
         rb.AddForce(new Vector3(horizontal * walkForce, 0, 0), walkForceMode);
         if (playerWantsToJump)              // Since it is active only once per frame, and FixedUpdate may not run in that frame!
         {
             rb.velocity = new Vector3(rb.velocity.x * slowDownAtJump, rb.velocity.y, rb.velocity.z);
             rb.AddForce(new Vector3(0, jumpImpulse, 0), jumpForceMode);
             playerWantsToJump = false;
         }
     }
 }
Ejemplo n.º 3
0
    void FixedUpdate()
    {
        // Handle jump.
        // NOTE: If instructed to jump, we'll check if we're grounded.
        if (playerWantsToJump && touchDetector.IsTouching())
        {
            rigidBody.AddForce(Vector3.up * JumpImpulse, ForceMode.Impulse);
        }

        // Set sideways velocity.
        rigidBody.velocity = new Vector3(sideSpeed, rigidBody.velocity.y, rigidBody.velocity.z);

        // Reset movement.
        playerWantsToJump = false;
        sideSpeed         = 0f;
    }
Ejemplo n.º 4
0
    private void Update()
    {
        // Keyboard events are tested each frame, so we should check them here.
        if (Input.GetKeyDown(KeyCode.Space))
        {
            playerWantsToJump = true;
        }

        if (td.IsTouching())
        {  // allow to walk and jump
           //float horizontal = Input.GetAxis("Horizontal");
            float   horizontal = Input.GetAxisRaw("Horizontal");
            float   vertical   = Input.GetAxisRaw("Vertical");
            Vector3 direction  = new Vector3(-horizontal, 0f, -vertical).normalized;

            if (direction.magnitude >= 0.1f)
            {
                float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg;
                float angle       = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
                transform.rotation = Quaternion.Euler(0f, angle, 0f);


                Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
                rb.AddForce(moveDir.normalized * walkForce, walkForceMode);
            }
            if (direction.magnitude == 0)
            {
                rb.velocity = new Vector3(rb.velocity.x * slowDown, rb.velocity.y * slowDown, rb.velocity.z * slowDown);
            }

            if (playerWantsToJump)
            {            // Since it is active only once per frame, and FixedUpdate may not run in that frame!
                rb.velocity = new Vector3(rb.velocity.x * slowDownAtJump, rb.velocity.y, rb.velocity.z);
                rb.AddForce(new Vector3(0, jumpImpulse, 0), jumpForceMode);
                playerWantsToJump = false;
            }
        }
    }
Ejemplo n.º 5
0
    void FixedUpdate()
    {
        if (playerWantsToDash)
        {
            Vector3 dashVelocity = Vector3.Scale(transform.forward, DashDistance * new Vector3(dashDrag, 0, dashDrag));
            rbody.AddForce(dashVelocity, ForceMode.Impulse);
            playerWantsToDash = false;
        }

        // move player
        rbody.MovePosition(rbody.position + inputs * Speed * Time.fixedDeltaTime);


        if (td.IsTouching())
        {     // allow to walk and jump
            if (playerWantsToJump)
            { // Since it is active only once per frame, and FixedUpdate may not run in that frame!
                rbody.velocity = new Vector3(rbody.velocity.x * slowDownAtJump, rbody.velocity.y, rbody.velocity.z);
                rbody.AddForce(new Vector3(0, jumpImpulse, 0), ForceMode.Impulse);
                playerWantsToJump = false;
            }
        }
    }
Ejemplo n.º 6
0
    void FixedUpdate()
    {
        inputs   = Vector3.zero;
        inputs.x = Input.GetAxis("Horizontal");
        inputs.z = Input.GetAxis("Vertical");


        if (playerWantsToDash)
        {
            rbody.gameObject.GetComponent <Animator>().Play("dash");

            Vector3 dashVelocity = Vector3.Scale(transform.forward, DashDistance * new Vector3(dashDrag, 0, dashDrag));
            rbody.AddForce(dashVelocity, ForceMode.Impulse);
            playerWantsToDash = false;
        }

        // move player
        inputs = transform.TransformDirection(inputs);
        if (Input.GetAxis("Horizontal") != 0)
        {
            rbody.MovePosition(rbody.position + inputs * Speed * Time.fixedDeltaTime);
        }
        if (Input.GetAxis("Horizontal").Equals(0))
        {
            rbody.MovePosition(rbody.position + inputs * Speed * Time.fixedDeltaTime);
        }

        if (td.IsTouching())
        {     // allow to walk and jump
            if (playerWantsToJump)
            { // Since it is active only once per frame, and FixedUpdate may not run in that frame!
                rbody.velocity = new Vector3(rbody.velocity.x * slowDownAtJump, rbody.velocity.y, rbody.velocity.z);
                rbody.AddForce(new Vector3(0, jumpImpulse, 0), ForceMode.Impulse);
                playerWantsToJump = false;
            }
        }
    }