# Keep velocity when jumping?

I have my script and it works very well, exempt that the character stops moving when jumping so you cant jump over objects. How can I fix this?

``````using UnityEngine;
using System.Collections;

public class Move : MonoBehaviour
{
CharacterController characterController;

public float speed = 6.0f;
public float jumpSpeed = 8.0f;
public float gravity = 20.0f;

private Vector3 moveDirection = Vector3.zero;

public float cspeed = 5.0F; //how fast the object should rotate
public GameObject cammera;

void Start()
{
characterController = GetComponent<CharacterController>();
Cursor.lockState = CursorLockMode.Locked;
}

void Update()
{
transform.Rotate(new Vector3(0, Input.GetAxis("Mouse X") * cspeed, 0));

cammera.transform.localRotation = Quaternion.identity;

if (characterController.isGrounded)
{

moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
moveDirection *= speed;

if (Input.GetButton("Jump"))
{
moveDirection.y = jumpSpeed;
}
}

// Apply gravity. Gravity is multiplied by deltaTime twice (once here, and once below
// when the moveDirection is multiplied by deltaTime). This is because gravity should be applied
// as an acceleration (ms^-2)
moveDirection.y -= gravity * Time.deltaTime;

// Move the controller
moveDirection = transform.TransformDirection(moveDirection);
characterController.Move(moveDirection * Time.deltaTime);

}

}
``````

That’s is because you pass a world space vector to `TransformDirection` function.
This function works with local space vectors and when you give it a world space vector you get weird results. You shouldn’t use `TransformDirection` here because it does not what you need.
I assume you want the character to move in the direction it looks at. To do that you need to use the character’s transform directional vectors such as `transform.forward` and `transform.right` and get a `moveDirection` that depends on current character orientation.

``````if (characterController.isGrounded)
{
moveDirection = transform.forward * Input.GetAxis("Vertical") + transform.right * Input.GetAxis("Horizontal");
moveDirection *= speed;
}
``````