- Home /
Movement in Mid Air WITH a reduced speed
I'm trying to emulate Half Life's movement, and in that game you can influence your movement in mid air, but only to a certain degree. I'm having a hard time doing that. You'll see also that I have 3 different speeds, one for the base movement, and then one to be removed from the base when the player is crouching, and one to be added while the player is sprinting. I'm sure there are better ways to do what I've done, but I haven't messed with it yet sadly. CURRENTLY, the player maintains their momentum JUST before the jump, and keeps that momentum in mid air. The problem is that I don't know how to influence my direction and velocity in mid air while keeping gravity normal. Below is my movement code. I appreciate any and all help. using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerMovement : MonoBehaviour { public float crouchSpeedToRemove = -3.0f; public float baseSpeed = 6.0F; public float runSpeedToAdd = 3.0F; public float jumpForce = 8.0F; public float gravity = 20.0F; /// <summary> /// The height of the character when not crouched. /// </summary> public float regularHeight; public float crouchHeight; private Vector3 moveDirection = Vector3.zero; private Vector3 runDirection = Vector3.zero; private Vector3 crouchDirection = Vector3.zero; private CharacterController charController; private bool isCrouched; void Start() { charController = GetComponent<CharacterController>(); } void Update() { if (charController.isGrounded) { moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); moveDirection = transform.TransformDirection(moveDirection); moveDirection *= baseSpeed; runDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); runDirection = transform.TransformDirection(runDirection); runDirection *= runSpeedToAdd; crouchDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); crouchDirection = transform.TransformDirection(crouchDirection); crouchDirection *= crouchSpeedToRemove; if (Input.GetButton("Jump"))//jump { moveDirection.y = jumpForce; } if (Input.GetKey(KeyCode.LeftShift) && !isCrouched)//sprint { charController.Move(runDirection * Time.deltaTime); } }//end of isGrounded if (Input.GetKey(KeyCode.LeftControl)) { charController.Move(crouchDirection * Time.deltaTime); charController.height = crouchHeight; isCrouched = true; } else { charController.height = regularHeight; isCrouched = false; } if (Input.GetKey(KeyCode.LeftShift) && !isCrouched)//sprint { charController.Move(runDirection * Time.deltaTime); } moveDirection.y -= gravity * Time.deltaTime;//subtracts gravity from the Y vector in the moveDirection vector 3 charController.Move(moveDirection * Time.deltaTime);//applies new gravity } }
PS i have no idea why my unity code is getting squished like it is, in the post preview it doesn't squish it at all!
Answer by yoyo696 · Jul 23, 2018 at 10:36 AM
Chek das ouf: but only to a certain degree. I'm having a hard time doing that. You'll see also that I have 3 different speeds, one for the base movement, and then one to be removed from the base when the player is crouching, and one to be added while the player is sprinting. I'm sure there are better ways to do what I've done, but I haven't messed with it yet sadly. CURRENTLY, the player maintains their momentum JUST before the jump, and keeps that momentum in mid air. The problem is that I don't know how to influence my direction and velocity in mid air while keeping gravity normal. Below is my movement code. I appreciate any and all help. using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerMovement : MonoBehaviour { public float crouchSpeedToRemove = -3.0f; public float baseSpeed = 6.0F; public float runSpeedToAdd = 3.0F; public float jumpForce = 8.0F; public float gravity = 20.0F; /// <summary> /// The height of the character when not crouched. /// </summary> public float regularHeight; public float crouchHeight; private Vector3 moveDirection = Vector3.zero; private Vector3 runDirection = Vector3.zero; private Vector3 crouchDirection = Vector3.zero; private CharacterController charController; private bool isCrouched; void Start() { charController = GetComponent<CharacterController>(); } void Update() { if (charController.isGrounded) { moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); moveDirection = transform.TransformDirection(moveDirection); moveDirection *= baseSpeed; runDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); runDirection = transform.TransformDirection(runDirection); runDirection *= runSpeedToAdd; crouchDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); crouchDirection = transform.TransformDirection(crouchDirection); crouchDirection *= crouchSpeedToRemove; if (Input.GetButton("Jump"))//jump { moveDirection.y = jumpForce; } if (Input.GetKey(KeyCode.LeftShift) && !isCrouched)//sprint { charController.Move(runDirection * Time.deltaTime); } }//end of isGrounded if (Input.GetKey(KeyCode.LeftControl)) { charController.Move(crouchDirection * Time.deltaTime); charController.height = crouchHeight; isCrouched = true; } else { charController.height = regularHeight; isCrouched = false; } if (Input.GetKey(KeyCode.LeftShift) && !isCrouched)//sprint { charController.Move(runDirection * Time.deltaTime); } moveDirection.y -= gravity * Time.deltaTime;//subtracts gravity from the Y vector in the moveDirection vector 3 charController.Move(moveDirection * Time.deltaTime);//applies new gravity } }
Your answer
Follow this Question
Related Questions
WebGl Mouse Look Problem 0 Answers
MY FPSE`S fps is not moving need help! I tried all solutions! 0 Answers
Carry forward velocity against mouse movements 1 Answer
Shooting does not affect the Health 0 Answers
Player Falls Much Slower While Moving 2 Answers