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.