- Home /
Maintaining forward momentum and control while jumping with character controller
Hello,
I am creating a 3D platformer and have created a character controller that allows the player to run and jump. The problem is when the player jumps all forward momentum is lost, the character just jumps straight vertically. I'm trying to have the player continue his/her forward momentum when jumping, and also allow them to control the character while in the air as that's a common/expected mechanic.
I've read several posts on Unity and Stack Overflow and can't find any similar questions/solutions. Can anyone help point out how I can accomplish this?
My code is almost all from Brackey's 3D movement tutorial with a few small customizations and animation code: (Link to his tutorial: https://www.youtube.com/watch?v=4HpC--2iowE).
While I understand how the existing code works (after many hours reading about Eulers and Quaternions), unfortunately I can't figure out how to both keep forward momentum while jumping and allow the player to control their position while in the air. Here is the code:
public class MovePlayer : MonoBehaviour
{
CharacterController controller;
Animator anim;
public Transform cam;
public float moveSpeed = 5.0f;
public float jumpHeight = 2f;
public float gravity = 18.0f;
public bool groundedPlayer;
public Vector3 jumpVector = Vector3.zero;
public float turnSmoothTime = 0.1f;
float turnSmoothVelocity;
void Awake()
{
controller = GetComponent<CharacterController>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
groundedPlayer = controller.isGrounded;
float x = Input.GetAxisRaw("Horizontal");
float z = Input.GetAxisRaw("Vertical");
Vector3 moveDirection = new Vector3(x, 0f, z).normalized;
if (moveDirection.magnitude >= 0.1f && groundedPlayer)
{
anim.SetBool("Moving", true);
float targetAngle = Mathf.Atan2(moveDirection.x, moveDirection.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
controller.Move(moveDir.normalized * moveSpeed * Time.deltaTime);
if (Input.GetButtonDown("Jump"))
{
jumpVector.y = Mathf.Sqrt(jumpHeight * 2f * gravity);
}
}
else
{
anim.SetBool("Moving", false);
}
jumpVector.y -= gravity * Time.deltaTime;
controller.Move(jumpVector * Time.deltaTime);
}
}
Thank you in advance for any help you can offer.
Solution: I had to move the groundedPlayer check from the "if (moveDirection.magnitude >= 0.1f && groundedPlayer)" line and instead to the "if (Input.GetButtonDown("Jump"))" line.
New code below.
public class $$anonymous$$ovePlayer : $$anonymous$$onoBehaviour
{
CharacterController controller;
Animator anim;
public Transform cam;
public float moveSpeed = 5.0f;
public float jumpHeight = 2f;
public float gravity = 18.0f;
public bool groundedPlayer;
public Vector3 jumpVector;
public float turnSmoothTime = 0.1f;
float turnSmoothVelocity;
void Awake()
{
controller = GetComponent<CharacterController>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
groundedPlayer = controller.isGrounded;
float x = Input.GetAxisRaw("Horizontal");
float z = Input.GetAxisRaw("Vertical");
Vector3 moveDirection = new Vector3(x, 0f, z).normalized;
if (moveDirection.magnitude >= 0.1f )
{
anim.SetBool("$$anonymous$$oving", true);
float targetAngle = $$anonymous$$athf.Atan2(moveDirection.x, moveDirection.z) * $$anonymous$$athf.Rad2Deg + cam.eulerAngles.y;
float angle = $$anonymous$$athf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
controller.$$anonymous$$ove(moveDir.normalized * moveSpeed * Time.deltaTime);
if (Input.GetButtonDown("Jump") && groundedPlayer)
{
jumpVector.y = $$anonymous$$athf.Sqrt(jumpHeight * 2f * gravity);
}
}
else
{
anim.SetBool("$$anonymous$$oving", false);
}
jumpVector.y -= gravity * Time.deltaTime;
controller.$$anonymous$$ove(jumpVector * Time.deltaTime);
}
}
Answer by ugotstoopt · Dec 08, 2020 at 06:49 AM
Also not sure why my code all shows up on a single line...
You should leave a space between text and code block for it to be placed in a code block e.g. without space between text and code = void Start() {}
With space between text =
void Start()
{
}
Answer by Llama_w_2Ls · Dec 08, 2020 at 07:45 AM
This line public Vector3 jumpVector = Vector3.zero;
means that jumpVector.x = 0 and jumpVector.z = 0;
This means that when you set the controller to move using the jumpVector on jump, it has no x or z magnitude. You need to modify the current move direction's y position, and not really setting the controller's move direction to another variable. @ugotstoopt
Thanks for the reply on this. I solved it by moving the location of the "groundedPlayer" check to the section looking for the 'jump' input.
Answer by Virtoze · Dec 19, 2021 at 09:17 AM
Hi I know this is a bit late lol. But I found the reason behind it. I was getting the same problem. The problem is the playermovement with speed, is under the statement "is grounded" which means when you jump you are not grounded, and therefore the playermovement with the speed, is not valid. I moved the "move = speed" line further down out of the "if grounded", and let it run in the void update as a constant, and it works flawlessly.
Hope this helps you and/or others!