Advertisement

3 Basic questions about my third person controller

Started by December 28, 2019 03:28 PM
2 comments, last by EBZ 5 years, 1 month ago

Hello friends, new to the forums, pleased to meet you all! I have 3 newbie question. I'm working on third person controls on Unity and only using an Xbox One controller.

Unable to jump while moving

1)
For now I got the camera working, the character moves towards the direction the camera is pointing at, and he jump while not moving.

The issue I got is when the character is moving, he can't jump and I can't figure out why. Let me share the controls snippet real quick:

private void Update () {


        if (controller.isGrounded) {

            // NOTE: Using Xbox One controller ONLY
            input          = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
            moveDirection  = new Vector3(input.x, 0.0f, input.y);
            inputNRM        = input.normalized;
        }


        UpdateRotation(); // Rotate first


        if (input.sqrMagnitude >= 0.20f) {
            currentSpeed = 8.0f;
           moveDirection = transform.forward * currentSpeed;
        }
        else currentSpeed = 0.0f;
        
        if (Input.GetButton("Jump") && controller.isGrounded) moveDirection.y = maxJumpVelocity;



        moveDirection.y -= gravity * Time.deltaTime;
        controller.Move(moveDirection * Time.deltaTime); // Using Character Controller
}

So I rotate the character and then attempt moving him forward relative to this new rotation.


Move speed relative to analog stick

2) I'm also trying to move him slowly if I'm pushing on the left analog stick softly, but the character seems to only change between two speeds. I'm sure I'm doing something wrong.

Character Rotating back to 0 degrees

3) I've also noticed that when I walk/run -180 degrees (backwards), if I let go of the analog stick, the character slerps back to 0 degrees, so he looks forward again. I can't think of a way to avoid this. I'll also share the UpdateRotation function:

    private void UpdateRotation () {

        float angle = 0.0f;

        if (input.magnitude >= 0.35f) {
            if (inputNRM != Vector2.zero) {
               angle = Mathf.Atan2(input.x, input.y) * Mathf.Rad2Deg + cameraTransform.eulerAngles.y;
            }
        }

        Quaternion current = transform.rotation;
        Quaternion target = Quaternion.AngleAxis(angle, transform.up);
        transform.rotation = Quaternion.Slerp(current, target, Time.deltaTime * 8.5f);

    }


Thanks all, hopefully I made sense.

For the first question, an obvious first step (if you haven't already done this) would be to use debug logging or some other method to determine 1) if/when a 'jump' button press is being detected, and 2) when the controller is or is not grounded. Since you can jump while not moving, that suggests an issue with grounding, or perhaps some other issue that isn't obvious from the code you posted.

For the second question, the first thing I'd check is the resolution of the controller input. Does the controller only register two speeds? Or is the controller providing finer input and is the speed being quantized for some other reason?

For the third question, the angle is left at 0 when there's no input, so the code seems to be working as designed. If you want the current orientation to persist when there's no input, one solution might be not to modify the orientation at all when there's no input.

Advertisement

Zakwayd, granted, your suggestions helped out. Thanks a lot.

This topic is closed to new replies.

Advertisement