- Home /
Question by
BeardedSloth · Aug 28, 2014 at 02:53 AM ·
c#fpscontrollerjump
Jumping and maintaining current velocity
Hi, I'm trying to make a simple first person controls, I want the movement direction you're moving when you jump to stay the same while in the air. Right now I am able to completely control my character in the air with direction and speed, what would be the easiest way to go about it.
Here is my code:
void Update () {
//camera rotation Left and Right
float rotLeftRight = Input.GetAxis("Mouse X") * mouseSensitivity;
transform.Rotate(0, rotLeftRight, 0);
//camera rotation up and down with range
verticalRotation -= Input.GetAxis("Mouse Y") * mouseSensitivity;
verticalRotation = Mathf.Clamp(verticalRotation, -upDownRange, upDownRange);
Camera.main.transform.localRotation = Quaternion.Euler(verticalRotation,0,0);
//movement
float forwardSpeed = Input.GetAxis("Vertical") * movementSpeed;
float sideSpeed = Input.GetAxis("Horizontal") * movementSpeed;
Vector3 speed = new Vector3 (sideSpeed, verticalVelocity, forwardSpeed);
//movement rotates with camera
speed = transform.rotation * speed;
//gravity
verticalVelocity += Physics.gravity.y * Time.deltaTime;
//jump
if(Input.GetButtonDown("Jump") && characterController.isGrounded){
verticalVelocity = jumpSpeed;
}
characterController.Move(speed * Time.deltaTime);
}
Comment
Answer by DugganSC · Mar 17, 2015 at 08:29 AM
Check the isGrounded
property of your controller to determine whether to apply the movement at the bottom.
Specifically, don't apply movement if the property is false. Alternately, you could allow it, but scale the vector down so that you can only modify your jump a little while in mid-air.