In-Air Directional Movement when Jumping

I’ve grown out of using the “ThirdPersonController” script for moving my character around, and decided to write my own.

The movement, gravity, and rotating the character to the direction they are headed is working just fine, except, the player isn’t able to move around when in the air (which I would like later for programming gliding)

The player’s jump is locked to whenever he was facing when he starts his jump. Here is the code I have so far


//SIMPLE PLATFORMING MOVEMENT//

var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;

var mesh : Transform;
private var moveDirection : Vector3 = Vector3.zero;
private var controller : CharacterController;

//FLAGS
var isAirborne : boolean = false;
var inAirMultiplier : float = 0.25;	

function CalculateMovement()
{
    var x : float = Input.GetAxis("Horizontal");
    var z : float= Input.GetAxis("Vertical"); 
        
	var jumpButtonPressed = Input.GetButtonDown("Jump"); //SMH
    controller = GetComponent(CharacterController);
    
   if (controller.isGrounded) 
    {
		isAirborne = false;
        var cameraTransform : Transform  = Camera.main.transform;
        
        //Get the forward vector from the camera
        var forward : Vector3 = cameraTransform.TransformDirection(Vector3.forward);
        forward.y = 0;
        forward = forward.normalized;
        
        // Right vector relative to the camera 
        var right = Vector3(forward.z, 0, -forward.x);
		
		//Make sure that the movement is relative to the camera
        moveDirection = x * right + z * forward;
		
		//Speed the Character up
        moveDirection *= speed;

		//MAKE SURE JUMP ALWAYS COME AFTER ROTATION!!!!
         if (jumpButtonPressed) 
        {
            moveDirection.y = jumpSpeed;
        }
	}
	else
	//If not on the ground
	{
		isAirborne = true;
		// Adjust additional movement while in-air
		//*****I THINK THE LOGIC NEEDS TO GO HERE!**********
    }	
	
	//Orient the Player Accordingly
	if (moveDirection != Vector3.zero)
	{
		transform.rotation = Quaternion.LookRotation(Vector3(moveDirection.x, 0, moveDirection.z));	
	}
}

function ApplyMovement()
{
    // Apply gravity
    moveDirection.y -= gravity * Time.deltaTime;
  	// Move the controller
    controller.Move(moveDirection * Time.deltaTime);
}
function Update() 
{
	CalculateMovement ();
	ApplyMovement();
}

1 Answer

1

It looks to me like you already have the code. I would remove the isGrounded condition, and use the same movement for both grounded and in-air, though perhaps with a reduced speed in the air, by multiplying speed by 0.25 or whatever factor you feel is right.

UPDATE: I see the problem... you are lumping in the Y axis with the X and Z movement. What you need to do is store the Y value of moveDirection at the start of CalculateMovement:

var y : float = moveDirection.y;

then assign it back after:

moveDirection *= speed
moveDirection.y = y;

This is because moveDirection is being continuously applied, based on its value from the frame before.

I removed the isGrounded check, but now, the character slowly falls, and jumping really doesn't even move the character an inch...

See my update above

We are now best friends. I would have never caught that...

Been looking for a solution to this for a while, thank you!