Jump to content

Controlling Character Controller Character Movement.


reepblue
 Share

Recommended Posts

For the past 4 months, of my game, I've been simply just applying velocity on my player/forcing the player to Jump. Since like 99% of my game is centered around throwing the player controller around, I decided now would be the best time to perfect the air movement.

Before, applying force/velocity to the player caused this motion. The player would reach a velocity limit and then just fall to the floor.

image.png.3b184a2bc67ffe86c302f9ce9f68bf0d.png

I wanted a nice arch. I tried setting up a predefined calculation but my push math was terrible. I noticed that if I held the forward key down I got my arch I wanted. Right now I have something like this:

image.png.41675a2423a22da49ae0864916c90e08.png

Here's my current code.

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void FPSPlayer::HandleCharacterController()
{
	if (GetStage()->ConsoleShowing() || m_bSuspendMovement)
		return;

	if (GetEntity()->GetPhysicsMode() == Entity::RigidBodyPhysics)
		return;

	float jumpinput = 0;
	Vec2 movement = Input::GetActionAxis(Input::ToActionAxis(ACTION_MOVEFORWARD, ACTION_MOVEBACKWARD, ACTION_MOVELEFT, ACTION_MOVERIGHT, GAMEPADAXIS_LSTICK));
	float speed = g_player_walkspeed;


	// Handle Crouching
	HandleCrouching();

	if (IsCrouched()) speed = g_player_walkspeed / 2;

	auto l = movement.Length();
	if (l > 0.0)
	{
		movement.x = (movement.x / l) * speed;
		movement.y = (movement.y / l) * speed;
	}

	// Handle Jumping
	if (ActionHit(ACTION_JUMP))
	{
		if (!GetEntity()->GetAirborne())
		{
			jumpinput = g_player_jumpforce;
			ChangeMovementState(CHARACTERSTATE_JUMPING);
			OnJump();

			if (movement.y != 0) movement.y = movement.y * 1.6;
			if (movement.x == 0 || movement.y == 0) OnPerStep();
		}
	}

	if (GetMovementState() == CHARACTERSTATE_FLYING)
	{
		auto force = GetVelocity().xz();
		UpdateMovement(force);
		GetEntity()->SetInput(0, force.y, force.x, 0, false, 0.50, 0.25, true);
	}
	else
	{
		UpdateMovement(movement);
		GetEntity()->SetInput(GetEyeAngles().y, movement.y, movement.x, jumpinput, m_bCrouched, 0.50, 0.25, true);
	}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
Vec3 PlayerActor::GetVelocity()
{
	Vec3 r = GetEntity()->GetVelocity();
	auto cc = GetEntity()->charactercontroller;
	if (cc != NULL)
	{
		r = cc->GetVelocity();
	}

	return r;
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void PlayerActor::Push(const int x, const int y, const int z)
{
	auto v = Vec3(x, y, z);
	SetVelocity(v);
	if (y != 0) ChangeMovementState(CHARACTERSTATE_FLYING);
}

 

What I have now is more acceptable that my previous code. What I'm asking is for any suggestions to improve this. Should I be using AddForce instead and just multiple the entity's mass? Also any information about if there are any differences between applying/getting the velocity from character controller instead of the entity would be nice. 

If I can't make anything better, I'm probably going to call it here.

  • Like 1

Cyclone - Ultra Game System - Component PreprocessorTex2TGA - Darkness Awaits Template (Leadwerks)

If you like my work, consider supporting me on Patreon!

Link to comment
Share on other sites

Ok, I think I got something decent. First, I redid my Push function now including a new vector and uint64_t member.

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void FPSPlayer::Push(const int x, const int y, const int z)
{
	m_vPushForce = Vec3(x, y, z);
	GetEntity()->SetVelocity(m_vPushForce.x, m_vPushForce.y, m_vPushForce.z);
	m_u64PushLaunchTime = GetStage()->GetTime();
	ChangeMovementState(CHARACTERSTATE_FLYING);
}

Added this to my UpdatePhysics function.

	// Stop moving me if we're landed and it's been a bit since we were last pushed.
	if (m_u64PushLaunchTime > 0)
	{
		if (GetStage()->GetTime() > m_u64PushLaunchTime + 100)
		{
			if (m_vPushForce != Vec3(0) && !IsAirbone())
			{
				m_vPushForce = Vec3(0);
				m_u64PushLaunchTime = 0;
			}
		}
	}

My new move function

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void FPSPlayer::HandleCharacterController()
{
	if (GetStage()->ConsoleShowing() || m_bSuspendMovement)
		return;

	if (GetEntity()->GetPhysicsMode() == Entity::RigidBodyPhysics)
		return;

	float jumpinput = 0;
	Vec2 movement = Input::GetActionAxis(Input::ToActionAxis(ACTION_MOVEFORWARD, ACTION_MOVEBACKWARD, ACTION_MOVELEFT, ACTION_MOVERIGHT, GAMEPADAXIS_LSTICK));
	float speed = g_player_walkspeed;

	// Handle Crouching
	HandleCrouching();

	if (IsCrouched()) speed = g_player_walkspeed / 2;

	auto l = movement.Length();
	if (l > 0.0)
	{
		movement.x = (movement.x / l) * speed;
		movement.y = (movement.y / l) * speed;
	}

	// Handle Jumping
	if (ActionHit(ACTION_JUMP))
	{
		if (!IsAirbone())
		{
			jumpinput = g_player_jumpforce;
			ChangeMovementState(CHARACTERSTATE_JUMPING);
			OnJump();

			if (movement.y != 0) movement.y = movement.y * 1.6;
			if (movement.x == 0 || movement.y == 0) OnPerStep();
		}
	}
	else if (m_vPushForce.y != 0)
	{
		jumpinput = m_vPushForce.y;
		m_vPushForce.y = 0;
		if (movement.y != 0) movement.y = movement.y * 1.6;
		if (movement.x == 0 || movement.y == 0) OnPerStep();
	}

	UpdateMovement(movement);

	if (m_vPushForce.x > 0 || m_vPushForce.z > 0)
	{
		movement += GetEntity()->GetVelocity(false).xz();
	}

	GetEntity()->SetInput(GetEyeAngles().y, movement.y, movement.x, jumpinput, m_bCrouched, 0.50, 0.25, true);
}

The player will actually slide a bit but this makes sense and feels more natural than a hard stop. Without any input this is fine but when I add any player input, it's too much.

Cyclone - Ultra Game System - Component PreprocessorTex2TGA - Darkness Awaits Template (Leadwerks)

If you like my work, consider supporting me on Patreon!

Link to comment
Share on other sites

My temp fix that'll probably leave as.

	if (m_vPushForce.x > 0 || m_vPushForce.z > 0)
	{
		movement = Vec2(0);
		movement += GetEntity()->GetVelocity(false).xz();
		//DMsg(movement.ToString());
	}

 

Cyclone - Ultra Game System - Component PreprocessorTex2TGA - Darkness Awaits Template (Leadwerks)

If you like my work, consider supporting me on Patreon!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...