- Home /
Question by
Misthra-Games · May 02, 2016 at 06:03 AM ·
c#movementjump
Cant move and Jump at the same time
Hey! I made my own movement and MouseLook Script, and I cant seem to move and jump at the same time.
Here the code: using UnityEngine; using System.Collections;
public class PlayerMovement : MonoBehaviour {
private float speed = 6f;
private float jumpForce = 16f;
private float gravity = 30f;
private Vector3 moveDir = Vector3.zero;
void Start () {
}
public void movementSpeed() {
if (Input.GetKey (KeyCode.LeftShift)) {
speed = 10f;
} else
speed = 6f;
}
void Update () {
CharacterController controller = gameObject.GetComponent<CharacterController> ();
movementSpeed ();
if (controller.isGrounded) {
moveDir = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
moveDir = transform.TransformDirection (moveDir);
moveDir *= speed;
if (Input.GetButtonDown ("Jump")) {
moveDir.y = jumpForce;
}
}
moveDir.y -= gravity * Time.deltaTime;
controller.Move (moveDir * Time.deltaTime);
}
}
Comment
Since you only apply the input to the moveDir when the CC is grounded, it's no wonder you can't move when in the air :)
If i take it out of the if statement gravity doesnt work
Answer by PRABHBIR123 · Jan 27, 2019 at 06:06 PM
@Misthra-Games check out this tutorial https://youtu.be/_93c-78CLak
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Multiple Cars not working 1 Answer
Extend simple movement script (C#) 1 Answer
[C#] Jump on slopes 1 Answer
I cant make my character jump ?,Why can't he jump ? 0 Answers