- Home /
In Air Movement
Hi. I'm currently working on my first 3D unity game and am having some trouble with the character movement. The player cannot change their velocity on the X and Z axis while in the air, and I wanted to change that. I've tried removing the if statement detecting that the player is grounded, but for some reason that makes the player unable to jump. Please advise.
CharacterController controller = GetComponent<CharacterController>();
AudioSource jumpSound = GetComponent<AudioSource>();
// is the controller on the ground?
if (controller.isGrounded)
{
isRunning = false;
//Feed moveDirection with input.
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
//Multiply it by speed.
if (Input.GetKey(KeyCode.LeftControl) && moveDirection.x != 0)
{
moveDirection *= speed*2;
isRunning = true;
} else
{
moveDirection *= speed;
}
//Jumping
if (Input.GetButton("Jump") && controller.isGrounded)
{
moveDirection.y = jumpSpeed;
jumpSound.Play();
isRunning = false;
} else
{
moveDirection.y = 0.1f;
}
}
//Applying gravity to the controller
moveDirection.y -= gravity * Time.deltaTime;
//Making the character move
controller.Move(moveDirection * Time.deltaTime);
Comment
Your answer
Follow this Question
Related Questions
Move while jump on board 0 Answers
3D Object Jump Glitch 0 Answers
In air movement troubles. 1 Answer
3d Space Movement 1 Answer
Trying to Add jump to this code. 1 Answer