While on the ground, the diagonal movement is clamped to the speed variable, but while in the air, one of two things happen:
either it remains unclamped, and the player can jump normal height but diagonal movement is enlarged ( from speed on x axis + speed on z axis / 2 or whatever the correct math is)
or the whole moveDirection Vector3 is clamped, and the player can jump normal height while stationary, the diagonal is clamped to speed, but the faster the player is going reduces the jump height (player gains less height the more X and Z movement because all 3 variables together cannot exceed the value of speed)
how would i just clamp the X and Z movement while allowing Y to remain unclamped, allowing normal sized jumps without abnormally fast diagonal movement?
as it stands, right now the most efficient way to move in the game is to bunny hop while moving diagonally…
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FPSinput : MonoBehaviour {
//Public and Private
public float speed = 9.0f;
public float gravity = 20.0f;
public float jumpPower = 8.0f;
private Vector3 moveDirection = Vector3.zero;
private CharacterController _charController;
private Vector3 moveDirectionY = Vector3.zero;
// Use this for initialization
void Start () {
_charController = GetComponent<CharacterController> ();
}
// Update is called once per frame
void Update () {
//if shift pressed, set speed to sprint
if (Input.GetKeyDown (KeyCode.LeftShift)) {
speed = 18.0f;
} else if (Input.GetKeyUp (KeyCode.LeftShift)) {
speed = 9.0f;
}
//Checks to see if grounded
if (_charController.isGrounded) {
//Movement vector3s
moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
moveDirection = Vector3.ClampMagnitude (moveDirection, speed);
moveDirection = transform.TransformDirection (moveDirection);
moveDirection *= speed;
//If space is pressed, jump
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpPower;
}
}
else {
//Movement for while not grounded
moveDirection = new Vector3 (Input.GetAxis ("Horizontal") * speed, moveDirection.y, Input.GetAxis ("Vertical") * speed);
moveDirection = Vector3.ClampMagnitude (moveDirection, speed);
moveDirection = transform.TransformDirection (moveDirection);
}
//Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
//Movement
_charController.Move (moveDirection * Time.deltaTime);
}
}
i’ve tried inserting Mathf.clamp(s) into the else{} moveDirection = new vector for the X and Z parts and it didn’t work.
i’ve also tried assigning the Y movement to a new vector 3 “moveDirectionY” and doing moveDirection = new vector (x, 0, Z) and moveDirectionY = new vector (0, Y, 0), and only clamping moveDirection, but that didn’t work
(i did put a new _charController.Move = (moveDirectionY * Time.DeltaTime) at the end of Update but it caused all X and Z momentum gained in the air to remain as constant movement on the ground, like sliding without friction)
any help is appreciated, but before anyone suggests it, i’m not switching to Rigidbody.