- Home /
Question by
ngotm_tn · Jun 12, 2015 at 08:43 PM ·
vector3movement scriptjumpingnormalized
Making jumping independent of normalized movement?
I've just gotten started with Unity, and I've been working on some basic 3D first-person controls and movement. The problem I've been struggling with is how to incorporate jumping without the y vector being counted as part of the final normalized movement vector. In other words, how can I keep an accurate diagonal speed without jumping having an effect?
I want to be able to jump while traveling in any direction on the x-z plane without my horizontal speed changing.
A stripped down version of my script with the general ideas:
float forwardSpeed = Input.GetAxisRaw ("Vertical") * speed;
float strafeSpeed = Input.GetAxisRaw ("Horizontal") * speed;
Vector3 movement = new Vector3(strafeSpeed, 0, forwardSpeed);
if (Input.GetButton ("Jump")) {
movement.y = jumpSpeed;
}
Player.Move (movement.normalized * Time.deltaTime);
Comment
Answer by FortisVenaliter · Jun 12, 2015 at 08:45 PM
You could just actually handle them separately. For example:
Vector3 horizontal = new Vector3(src.x, 0, src.z);
Vector3 vertical = new Vector3(0, src.y, 0);
Vector3 final = horizontal.normalized + vertical;