- Home /
Need helping getting a character controller to jump
So far I have a character controller that enables me to move around, sprint and crouch (no animation) , but I am unable to get the controller to jump. I know 100% the character is getting the input to jump, and the movement vector is around ~40 on the Y axis, so the player should be moving. The problem is, nothing happens. The player can still move around, and fall of ledges, but nothing happens when I press space. Here is my code:
using UnityEngine;
public class KeyboardMovement : MonoBehaviour
{
private CharacterController controller;
public float walkSpeed;
public float sprintSpeed;
public float crouchSpeed;
public float jumpHeight;
Vector3 up = Vector3.zero;
Vector3 movement = Vector3.zero;
Vector3 forward = Vector3.zero;
Vector3 sideways = Vector3.zero;
void Start()
{
controller = GetComponent<CharacterController>();
}
void Update()
{
float speed = walkSpeed;
//If crouching, set speed to crouch speed. This ovverides sprinting
if (SingletonSettings.GetKey(SingletonSettings.Keybindings.crouch))
speed = crouchSpeed;
//Else if sprinting, set speed to sprint speed
else if (SingletonSettings.GetKey(SingletonSettings.Keybindings.sprint))
speed = sprintSpeed;
//Create vectors for movement
forward = transform.TransformDirection(Vector3.forward) * Input.GetAxis("Vertical");
sideways = transform.TransformDirection(Vector3.right) * Input.GetAxis("Horizontal");
//up = SingletonSettings.GetKey(SingletonSettings.Keybindings.jump) && controller.isGrounded ? Vector3.up * jumpHeight : Vector3.zero;
movement = (up * 100) + ((forward + sideways) * 10 * Time.deltaTime * speed);
//Combine vectors and Multiply by DeltaTime to ensure smooth movement at different framerates.
//Also multiply by 10*speed to ensure correct speed in different states
if (controller.isGrounded && Input.GetKey(KeyCode.Space))
{
movement.y = jumpHeight*50 * Time.deltaTime;
}
controller.SimpleMove(movement);
}
void OnGUI()
{
GUILayout.Label('\n' + Newtonsoft.Json.JsonConvert.SerializeObject(movement.y, Newtonsoft.Json.Formatting.Indented, new Newtonsoft.Json.JsonSerializerSettings
{
ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
}));
}
}
Have you tried using a Debug.Log statement to check if the jump is being triggered correctly? $$anonymous$$aybe you could try that :) Also i have always used CharacterController.$$anonymous$$ove because simple$$anonymous$$ove didnt really work for me when trying jumps. In your case i would use $$anonymous$$ove, the performance will be nearly the same and $$anonymous$$ove is suggested for jumps, flights etc.
Here is an example of how to implement jumps
if (controller.isGrounded && Input.GetButton("Jump")) {
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.$$anonymous$$ove(moveDirection * Time.deltaTime);
Answer by Kim-Nobre · May 12, 2019 at 05:59 PM
If you want to jump you should use Move instead of SimpleMove. Someone already answered that over here: https://answers.unity.com/questions/7942/simplemove-jump.html
Your answer
Follow this Question
Related Questions
3D Character Controller slowing down the higher the slope angle (both up and down) 1 Answer
C# Very Basic Character Controller Script 2 Answers
How to get our character controller script to make our player move? 1 Answer
Making a bubble level (not a game but work tool) 1 Answer
8-Axis 3D Top Down Movement 1 Answer