Air Control While Jumping?
so i'm trying to make a 3rd person platformer. everything works absolutely fine as it is. but what i want to be able to do is control the players direction while he is in the air/jumping. i have tried a lot of things so far and done a bit of research. i just cant seem to figure is out.. now i am only learning i an very new to coding. im just hoping that someone can push me in the right direction to get this to work. Thank-you!
[Header("Speed/Movement Settings")]
public float crouchSpeed = 2.5f;
public float walkSpeed = 5f;
public float runSpeed = 10f;
public float maxSlopeLimit = 45f;
private float speed; //the walk speed of the player
[Header("Jumping/Gravity Settings")]
public bool doubleJump; //bool for single of double jump
public bool airMove;
public float jumpForce = 15f; //how fast the player will move up when jumping
public float gravity = 35f; //how strong/fast gravity will pull you down
public float airControl = 0.25f;
[Header("Camera Settings")]
public GameObject cam; //the camera used for the player. mainly here for the Up/Down look and Clamp.
public float lookSensitivityX = 2f; // sets the sesitvity of the mouse Left/Right
public float lookSensitivityY = 0.75f; // sets the sesitvity of the mouse Up/Down
public float minLookY = -60f; //clamps the player look Down rotaion minimum
public float maxLookY = 75f; //clamps teh player look Up rotaton maximum
Vector3 moveDir = Vector3.zero;
int jumpTimes; //how many times the player will be able to jump in the air
float rotY; //the rotaion of the camera UP/DOWN
float rotX; //the rotation of the camera LEFT/RIGHT
void Update () // happens every frame
{
CharacterController controller = gameObject.GetComponent<CharacterController>(); // gets the character controller attached to the player.
float ih = Input.GetAxisRaw ("Horizontal"); // get the input of the horizontal axis (X)
float iv = Input.GetAxisRaw ("Vertical"); // gets the input on the verticle axis (Z)
if (controller.isGrounded) // if the player is on the ground
{
if (Input.GetKey (KeyCode.LeftShift)) //if the Left Shift Key is Held Down Speed will = to Run Speed
{
speed = runSpeed;
controller.center = Vector3.zero;
}
else if (Input.GetKey (KeyCode.C)) // but if the C key is held down speed will = the couch speed
{
speed = crouchSpeed;
}
else //if neither of the above keys are being held down speed will = to the defualt Walk speed
{
speed = walkSpeed;
controller.center = Vector3.zero;
}
moveDir = new Vector3 (ih, 0, iv).normalized; //seting where we want the axis to be modified and how
moveDir = transform.rotation * moveDir.normalized; // allows the player to be able to rotate and the same speed he moves( i think haha )
moveDir *= speed; // sets how fast the player will move
jumpTimes = 0; // set jump time to 0 so you are able to jump again
}
if (!doubleJump) //checks if the double jump bool is FALSE
{
//Debug.Log ("NormalJump"); // will show if single jump is active (in the console)
JumpFunc (); // find and moves through the single jump function
}
if (doubleJump) //checks if the double jump bool is TRUE
{
//Debug.Log ("DoubleJump"); // will show if double jump is active (in the console)
DoubleJumpFunc (); // find and moves through the double jump function
}
moveDir.y -= gravity * Time.deltaTime; // this will move the player downward at the speed on gravity.
controller.Move(moveDir * Time.deltaTime); // this will allow the player to actually be able to move.
Debug.Log (speed); // will show the speed of the player (in the console)
controller.slopeLimit = maxSlopeLimit; // this will set the players slope limit to a specified ammount.
}
void LateUpdate() //happens every other frame?
{
rotX = Input.GetAxis ("Mouse X") * lookSensitivityX; //Sets up the mouse to look left and right
rotY -= Input.GetAxis ("Mouse Y") * lookSensitivityY; //Sets up the mouse to look up and down
rotY = Mathf.Clamp (rotY, minLookY, maxLookY); //Clamps the up and down rotation
transform.Rotate (0, rotX, 0); //Allows you to look left and right
cam.transform.localRotation = Quaternion.Euler(rotY, 0, 0); //Allows you to look up and down (with boundries -- Clamp)
}
void JumpFunc() //single jump function
{
if (jumpTimes < 1) //see if the amount of times you have jumped is below 1
{
if (Input.GetButtonDown ("Jump")) //see if the space bar has been pressed
{
moveDir.y = jumpForce; // move you upwards ad the speed of "jump force"
jumpTimes += 1; // +'s 1 on to the jump time so then it will be 1 meaning youll be able to jump one more time which will then make jump time = 2 which is not lower then 2. will not be able to jump again till on the ground
}
}
}
void DoubleJumpFunc() //double jump function
{
if (jumpTimes < 2) //see if the amount of times you have jumped is below 2
{
if (Input.GetButtonDown ("Jump")) //see if the space bar has been pressed
{
moveDir.y = jumpForce; // move you upwards ad the speed of "jump force"
jumpTimes += 1; // +'s 1 on to the jump time so then it will be 1 meaning youll be able to jump one more time which will then make jump time = 2 which is not lower then 2. will not be able to jump again till on the ground
}
}
}
}
Answer by TheyLeftMe4Dead · Jan 16, 2017 at 04:50 AM
The simple answer to your question is to have a separate function be called every frame (via Unity's built-in Update function). This function would allow the user to move left/right independent of jumping. From what I understand, right now you check if the user has pressed the left/right key after/before the jumping script runs. You need to constantly check if the user has pressed the left/right key rather than waiting until your jump happens.
Your answer
Follow this Question
Related Questions
Animations are not playing with certain directions? 1 Answer
Top Down Character - Face direction of movement? 0 Answers
No Gravity Jump 0 Answers
Control Air movement with character controller 0 Answers
Bugs on move in air(falling); 0 Answers