How would i clamp X and Z without clamping Y?
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.
Answer by Glurth · Feb 10, 2017 at 06:00 PM
Uncompiled, example only: Check's the movement Vector3's XY component's magnitude against "speed": and if greater, reduces it to "speed"
float XYmag = Mathf.Sqrt(movement.x*movement.x + movement.y*movement.y); //good 'ol Pythagorus
if(XYMag>speed) //if we don't go in here- no need to clamp
{
movementNormalizedXY = new Vector3( movement.x/XYmag, movement.y/XYmag, movement.z); //after this operation the XY component's magnitude will be 1.0f
movementAtSpeedXY = new Vector3( movementNormalizedXY.x * speed, movementNormalizedXY.y * speed, movement.z); //multiply that 1.0 magnitude by our speed
movement= movementAtSpeedXY; //done- assign back to movement
}
Your answer
Follow this Question
Related Questions
Clamp a Vector3 position to an arbitrary line segment 1 Answer
ClampMagnitude while ignoring a direction 0 Answers
Simple 3d controls 0 Answers