Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Character controller and gradual air control

Discussion in 'Scripting' started by Hertzole, Jan 1, 2018.

  1. Hertzole

    Hertzole

    Joined:
    Jul 27, 2013
    Posts:
    416
    Hello

    I'm trying to solve the problem with air control with my character controller. Everything I find is either full air control or no air control. Nothing in-between. And everything I've tried myself just results in some weird results and/or just stopping on a dime mid-air. What I ideally want is to be able to control the amount of air control with a value that goes from 0 (no air control at all) to 1 (full air control).

    Here my current code.
    Code (CSharp):
    1. // Get the move direction from the movement input X and Y (on the Z axis).
    2. moveDirection = new Vector3(movementInput.x, moveDirection.y, movementInput.y);
    3. // If movement input Y is above 0, we're moving forward, so apply forward move speed.
    4. // Else if below 0, we're moving backwards, so apply backwards move speed.
    5. if (movementInput.y > 0)
    6.     moveDirection.z *= moveSpeed.ForwardSpeed;
    7. else
    8.     moveDirection.z *= moveSpeed.BackwardsSpeed;
    9.  
    10. // Apply the sideways movement speed to the X movement.
    11. moveDirection.x *= moveSpeed.SidewaysSpeed;
    12.  
    13. // Make sure we're moving in the direction the transform is facing.
    14. moveDirection = PlayerTransform.TransformDirection(moveDirection);
    15.  
    16. // Move the player using the character controller.
    17. CharacterController.Move(moveDirection * Time.deltaTime);
    The code is currently not set up to support any control over air control because I don't understand how to do such a thing.
    And I know about the "take the thing out of is grounded check etc". That is not what I want, to an extent. I want to still be able to control my player while in the air but not as much as on the ground.

    So what have I tried?
    Multiplying the moveDirection with airControl (that goes from 0 to 1). But that just stops the player mid-air.
    Saving the velocity when jumping and using that to multiply the moveDirection with. That just results in some weird results.

    I am not sure what to do from here so I come here to ask for help. Any help regarding this is appreciated!
     
  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    Air control is simple just a check. If you are grounded or not and adjust the speed accordingly.

    Code (CSharp):
    1. bool grounded = CharacterController.isGround;
    2. if (movementInput.y > 0)
    3.     moveDirection.z *= moveSpeed.ForwardSpeed * ((grounded) ? 1f : 0.25f);
    4. else
    5.     moveDirection.z *= moveSpeed.BackwardsSpeed * ((grounded) ? 1f : 0.25f);
    6.  
    7. moveDirection.x *= movedSpeed.SidewaysSpeed * ((grounded) ? 1f : 0.25f);
    8.  
     
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Just to add on .. I'd say make sure you're not allowing modification of the 'y' axis while you're in the air; it sounds like maybe you were, if you can stop in the air.
     
  4. Hertzole

    Hertzole

    Joined:
    Jul 27, 2013
    Posts:
    416
    This code, again, results in the player stopping mid-air. Let's say I have my air control set to 0, which I want to be no air control. That stops the player mid-air on the X and Z axis. I'm pretty certain that this is caused by the character controller not keeping the velocity from frame to frame and then multiplying the moveDirection axis with 0, it will just be 0, thus stopping the player mid-air. The same if I have a value of 0.5, for example. It will slow down in the air. And if I release the keys, the player just stops.
    Now when I think about it, I should probably have included that I want the player to keep the velocity.

    I didn't really mention it, but I don't touch the Y axis, so the player does fall down, but the X/Z axis just stop mid-air (or slow down, depending on my air control value).
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Ah, okay, I see -- not stuck in the air :) That's good. heh
     
  6. Hertzole

    Hertzole

    Joined:
    Jul 27, 2013
    Posts:
    416
    So I did a thing.
    I now have a variable I can play with while in the air. But how I apply gradual air control to is still a very big mystery to me.
    This is the updated code.
    Code (CSharp):
    1. if(isGrounded)
    2. {
    3.     // Get the move direction from the movement input X and Y (on the Z axis).
    4.     moveDirection = new Vector3(movementInput.x, moveDirection.y, movementInput.y);
    5.     // If movement input Y is above 0, we're moving forward, so apply forward move speed.
    6.     // Else if below 0, we're moving backwards, so apply backwards move speed.
    7.     if (movementInput.y > 0)
    8.         moveDirection.z *= moveSpeed.ForwardSpeed;
    9.     else
    10.         moveDirection.z *= moveSpeed.BackwardsSpeed;
    11.    
    12.     // Apply the sideways movement speed to the X movement.
    13.     moveDirection.x *= moveSpeed.SidewaysSpeed;
    14.    
    15.     // Make sure we're moving in the direction the transform is facing.
    16.     moveDirection = PlayerTransform.TransformDirection(moveDirection);
    17.     groundVelocity = CharacterController.velocity;
    18. }
    19. else
    20. {
    21.     moveDirection = new Vector3(groundVelocity.x, moveDirection.y, groundVelocity.z);
    22. }
    Right now the player has NO air control at all. And again, I want to modify the current velocity and apply a gradual air control to it without being sent to space and back (by multiplying with itself, which I've done a few times now).