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();
}
I removed the isGrounded check, but now, the character slowly falls, and jumping really doesn't even move the character an inch...
– SrBilyonSee my update above
– Steven-WalkerWe are now best friends. I would have never caught that...
– SrBilyonBeen looking for a solution to this for a while, thank you!
– andrew_cs