Esempio n. 1
0
    void Update()
    {
        float inputTurn    = Input.GetAxis("Horizontal");
        float inputForward = Input.GetAxis("Vertical");
        bool  inputJump    = Input.GetButtonDown("Jump");



        //onCollisionXXX() doesn't always work for checking if the character is grounded from a playability perspective
        //Uneven terrain can cause the player to become technically airborne, but so close the player thinks they're touching ground.
        //Therefore, an additional raycast approach is used to check for close ground
        bool isGrounded = grounded || CharacterCommon.CheckGroundNear(this.transform.position, jumpableGroundNormalMaxAngle, 0.85f, 0f, out closeToJumpableGround);

        if (inputJump && (isGrounded || !doubleJumped))
        {
            rbody.velocity = new Vector3(rbody.velocity.x, 5, rbody.velocity.z);
            if (!isGrounded)
            {
                doubleJumped = true;
            }
        }

        anim.SetFloat("velx", inputTurn);
        anim.SetFloat("vely", inputForward);
        anim.SetBool("isFalling", !isGrounded);
    }
    void OnAnimatorMove()
    {
        Vector3    newRootPosition;
        Quaternion newRootRotation;

        bool isGrounded = IsGrounded || CharacterCommon.CheckGroundNear(this.transform.position, jumpableGroundNormalMaxAngle, 0.85f, 0f, out closeToJumpableGround);

        if (isGrounded)
        {
            //use root motion as is if on the ground
            newRootPosition = anim.rootPosition;
        }
        else
        {
            //Simple trick to keep model from climbing other rigidbodies that aren't the ground
            newRootPosition = new Vector3(anim.rootPosition.x, this.transform.position.y, anim.rootPosition.z);
        }

        //use rotational root motion as is
        newRootRotation = anim.rootRotation;

        //TODO Here, you could scale the difference in position and rotation to make the character go faster or slower

        this.transform.position = newRootPosition;
        this.transform.rotation = newRootRotation;
    }
    void FixedUpdate()
    {
        float inputForward = 0f;
        float inputTurn    = 0f;

        // input is polled in the Update() step, not FixedUpdate()
        // Therefore, you should ONLY use input state that is NOT event-based in FixedUpdate()
        // Input events should be handled in Update(), and possibly passed on to FixedUpdate() through
        // the state of the MonoBehavior
        if (cinput.enabled)
        {
            inputForward = cinput.Forward;
            inputTurn    = cinput.Turn;
        }

        //onCollisionStay() doesn't always work for checking if the character is grounded from a playability perspective
        //Uneven terrain can cause the player to become technically airborne, but so close the player thinks they're touching ground.
        //Therefore, an additional raycast approach is used to check for close ground
        if (CharacterCommon.CheckGroundNear(this.transform.position, jumpableGroundNormalMaxAngle, 1.5f, 1f, out closeToJumpableGround))
        {
            isGrounded = true;
        }


        anim.SetFloat("velx", inputTurn);
        anim.SetFloat("vely", inputForward);
        anim.SetBool("isFalling", !isGrounded);
    }
    // Update is called once per frame
    void Update()
    {
        bool isGrounded = IsGrounded || CharacterCommon.CheckGroundNear(this.transform.position, jumpableGroundNormalMaxAngle, 0.1f, 1f, out closeToJumpableGround);

        if (isGrounded)
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                Vector3 jump = new Vector3(0.0f, 150.0f, 0.0f);
                rb.AddForce(jump * jumpHeight, ForceMode.Impulse);
                anim.SetBool("is_in_air", true);
            }
            else
            {
                anim.SetBool("is_in_air", false);
            }
        }

        float moveHorizontal = Input.GetAxisRaw("Horizontal");
        float moveVertical   = Input.GetAxisRaw("Vertical");
        // logic for compatibility for third person camera
        Vector3 direction = new Vector3(moveHorizontal, 0.0f, moveVertical).normalized;



        //do some filtering of our input as well as clamp to a speed limit
        float movespeed = Mathf.Max(Mathf.Abs(moveHorizontal), Mathf.Abs(moveVertical));

        filteredForwardInput = Mathf.Clamp(Mathf.Lerp(filteredForwardInput, movespeed,
                                                      Time.deltaTime * forwardInputFilter), -forwardSpeedLimit, forwardSpeedLimit);

        // test for moving using character controller
        if (direction.magnitude >= 0.1f)
        {
            float targetAngle   = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
            float smoothedAngle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
            transform.rotation = Quaternion.Euler(0f, smoothedAngle, 0f);
            Vector3 moveDirection = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
            //controller.Move(moveDirection.normalized * speed * Time.deltaTime);
            rb.MovePosition(rb.position + moveDirection.normalized * speed * Time.deltaTime);
        }

        if (Input.GetKeyUp(KeyCode.Escape))
        {
            if (!gameEnded)
            {
                PauseMenuToggle pauseMenuScript = InGameMenu.GetComponent <PauseMenuToggle>();
                pauseMenuScript.PauseMenuInteraction();
            }
        }

        anim.SetFloat("velocity", filteredForwardInput);
    }
    void FixedUpdate()
    {
        float inputForward = 0f;
        float inputTurn    = 0f;

        if (cinput.enabled)
        {
            inputForward = cinput.Forward;
            inputTurn    = cinput.Turn;
        }

        //switch turn around if going backwards
        if (inputForward < 0f)
        {
            inputTurn = -inputTurn;
        }

        //onCollisionStay() doesn't always work for checking if the character is grounded from a playability perspective
        //Uneven terrain can cause the player to become technically airborne, but so close the player thinks they're touching ground.
        //Therefore, an additional raycast approach is used to check for close ground
        if (CharacterCommon.CheckGroundNear(this.transform.position, jumpableGroundNormalMaxAngle, 0.1f, 1f, out closeToJumpableGround))
        {
            isGrounded = true;
        }

        //this.transform.Translate(Vector3.forward * cinput.Forward * Time.deltaTime * forwardMaxSpeed);
        //this.transform.Rotate(Vector3.up, cinput.Turn * Time.deltaTime * turnMaxSpeed);

        //It's supposed to be safe to not scale with Time.deltaTime (e.g. framerate correction) within FixedUpdate()
        //If you want to make that optimization, you can precompute your velocity-based translation using Time.fixedDeltaTime
        //We use rbody.MovePosition() as it's the most efficient and safest way to directly control position in Unity's Physics
        rbody.MovePosition(rbody.position + this.transform.forward * inputForward * Time.deltaTime * forwardMaxSpeed);
        //Most characters use capsule colliders constrained to not rotate around X or Z axis
        //However, it's also good to freeze rotation around the Y axis too. This is because friction against walls/corners
        //can turn the character. This errant turn is disorienting to players.
        //Luckily, we can break the frozen Y axis constraint with rbody.MoveRotation()
        //BTW, quaternions multiplied has the effect of adding the rotations together
        rbody.MoveRotation(rbody.rotation * Quaternion.AngleAxis(inputTurn * Time.deltaTime * turnMaxSpeed, Vector3.up));


        //anim.SetFloat("velx", inputTurn);
        anim.SetFloat("vely", inputForward);
        anim.SetBool("isFalling", !isGrounded);


        //clear for next OnCollisionStay() callback
        isGrounded = false;
    }
    void FixedUpdate()
    {
        float inputForward = 0f;
        float inputTurn    = 0f;

        if (cinput.enabled)
        {
            inputForward = cinput.Forward;
            inputTurn    = cinput.Turn;
        }

        //onCollisionXXX() doesn't always work for checking if the character is grounded from a playability perspective
        //Uneven terrain can cause the player to become technically airborne, but so close the player thinks they're touching ground.
        //Therefore, an additional raycast approach is used to check for close ground
        bool isGrounded = IsGrounded || CharacterCommon.CheckGroundNear(this.transform.position, jumpableGroundNormalMaxAngle, 0.85f, 0f, out closeToJumpableGround);


        anim.SetFloat("velx", inputTurn);
        anim.SetFloat("vely", inputForward);
        anim.SetBool("isFalling", !isGrounded);
    }
Esempio n. 7
0
    // Update is called once per frame
    void Update()
    {
        //onCollisionStay() doesn't always work for checking if the character is grounded from a playability perspective
        //Uneven terrain can cause the player to become technically airborne, but so close the player thinks they're touching ground.
        //Therefore, an additional raycast approach is used to check for close ground
        if (CharacterCommon.CheckGroundNear(this.transform.position, jumpableGroundNormalMaxAngle, 0.1f, 1f, out closeToJumpableGround))
        {
            
 isGrounded = true;
        }

        //Horizontal
        if (Input.GetButtonDown("Horizontal"))
        {
            isTurning = true;
            anim.SetBool("turn", isTurning);
        }


        if (Input.GetButton("Horizontal"))
        {
            var x = Input.GetAxis("Horizontal") * Time.deltaTime * 150f;
            transform.Rotate(0, x, 0);
        }

        if (Input.GetButtonUp("Horizontal"))
        {
            isTurning = false;
            anim.SetBool("turn", isTurning);
        }

        //Shift
        if (Input.GetButtonDown("Shift"))
        {
            shiftPressed = true;
            anim.SetBool("shift", shiftPressed);
        }

        if (Input.GetButtonUp("Shift"))
        {
            shiftPressed = false;
            anim.SetBool("shift", shiftPressed);
        }

        //Vertical
        if (Input.GetButtonDown("Vertical"))
        {
            anim.SetFloat("vert", 1f);
        }

        if (Input.GetButton("Vertical"))
        {
            if (shiftPressed)
            {
                speed = 1.7f;
            }
            else
            {
                speed = 1.3f;
            }
            var z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
            transform.Translate(0, 0, Mathf.Abs(z));
        }

        if (Input.GetButtonUp("Vertical"))
        {
            anim.SetFloat("vert", 0f);
        }

        //Other Functions
        //if (Input.GetKeyDown(KeyCode.C))
        //{
        //    anim.SetTrigger("cry");
        //}

        //if (Input.GetButtonDown("Fire1")) {
        //    Vector3 dir = this.transform.position;
        //    anim.SetTrigger("atk");
        //    dir = -dir.normalized;
        //    //rb.AddForce(dir * 1000);
        //}

        if (Input.GetButtonDown("Jump"))
        {
            if (isGrounded)
            {
                anim.SetTrigger("jump");
                rb.AddForce(transform.up * 200);
            }
        }

        anim.SetBool("isGrounded", isGrounded);
        isGrounded = false;
    }
Esempio n. 8
0
    void Update()
    {
        float inputForward         = 0f;
        float inputTurn            = 0f;
        bool  inputAction          = false;
        bool  doButtonPress        = false;
        bool  doMatchToButtonPress = false;


        if (cinput.enabled)
        {
            inputForward = cinput.Forward;
            inputTurn    = cinput.Turn;
            inputAction  = cinput.Action;
        }

        //onCollisionXXX() doesn't always work for checking if the character is grounded from a playability perspective
        //Uneven terrain can cause the player to become technically airborne, but so close the player thinks they're touching ground.
        //Therefore, an additional raycast approach is used to check for close ground.
        //This is good for allowing player to jump and not be frustrated that the jump button doesn't
        //work
        bool isGrounded = IsGrounded || CharacterCommon.CheckGroundNear(this.transform.position, jumpableGroundNormalMaxAngle, 0.1f, 1f, out closeToJumpableGround);



        float buttonDistance     = float.MaxValue;
        float buttonAngleDegrees = float.MaxValue;

        if (buttonPressStandingSpot != null)
        {
            buttonDistance     = Vector3.Distance(transform.position, buttonPressStandingSpot.transform.position);
            buttonAngleDegrees = Quaternion.Angle(transform.rotation, buttonPressStandingSpot.transform.rotation);
        }

        if (inputAction)
        {
            Debug.Log("Action pressed");

            if (buttonDistance <= buttonCloseEnoughForMatchDistance)
            {
                if (buttonDistance <= buttonCloseEnoughForPressDistance &&
                    buttonAngleDegrees <= buttonCloseEnoughForPressAngleDegrees)
                {
                    Debug.Log("Button press initiated");

                    doButtonPress = true;
                }
                else
                {
                    // TODO UNCOMMENT THESE LINES FOR TARGET MATCHING
                    // Debug.Log("match to button initiated");
                    // doMatchToButtonPress = true;
                }
            }
        }


        // TODO HANDLE BUTTON MATCH TARGET HERE



        anim.SetFloat("velx", inputTurn);
        anim.SetFloat("vely", inputForward);
        anim.SetBool("isFalling", !isGrounded);
        anim.SetBool("doButtonPress", doButtonPress);
        anim.SetBool("matchToButtonPress", doMatchToButtonPress);
    }