- Home /
How to make your character jump?
I watched a video that taught me how to make a character move around but they didn't say how to make the character jump and after get some weird results while testing to add a rigidbody I have given up on trying to figure it out myself and the internet hasn't been very helpful.
Here's the script btw:
public float sensitivity = 5f;
public float speedFaster = 10f;
public float normalSpeed = 5f;
float speed;
CharacterController player;
public GameObject eyes;
float moveLR;
float moveFB;
float rotX;
float rotY;
void Start () {
float speed = normalSpeed;
player = GetComponent<CharacterController>();
}
void Update ()
{
if (Input.GetKey(KeyCode.LeftShift))
{
speed = speedFaster;
}
else
{
speed = normalSpeed;
}
moveFB = Input.GetAxis("Vertical") * speed;
moveLR = Input.GetAxis("Horizontal") * speed;
rotX = Input.GetAxis("Mouse X") * sensitivity;
rotY = Input.GetAxis("Mouse Y") * sensitivity;
Vector3 Movement = new Vector3(moveLR, 0, moveFB);
Movement = transform.rotation * Movement;
transform.Rotate(0, rotX, 0);
eyes.transform.Rotate(-rotY, 0, 0);
player.Move(Movement * Time.deltaTime);
}
Answer by YBtheS · Jul 09, 2018 at 12:21 AM
The Unity Scripting API has a perfect example: CharacterController.Move. This only has an example in JavaScript so make sure that you set the language appropriately. You should be able to get the general idea from it.
You need to have a jump force variable (like public float jumpForce = anyNumber
). After that, you need to check if you are on the ground (unless you want to be able to jump midair), then check if the space bar (or whatever your jump key is) has been pressed, then account for gravity, and finally send that to the player.Move() method. The new script should look something like this:
public float sensitivity = 5f;
public float speedFaster = 10f;
public float normalSpeed = 5f;
public float jumpForce = 10f; //You can change this number to whatever works right for your game.
public float gravity = 1f; //Same with this variable. I just choose random numbers. They most
//most likely won't look right
float speed;
CharacterController player;
public GameObject eyes;
float moveUD
float moveLR;
float moveFB;
float rotX;
float rotY;
void Start () {
float speed = normalSpeed;
player = GetComponent<CharacterController>();
}
void Update ()
{
if (Input.GetKey(KeyCode.LeftShift))
{
speed = speedFaster;
}
else
{
speed = normalSpeed;
}
if (Input.GetKeyDown(KeyCode.Space))
{
moveUD += jumpForce;
}
else
{
moveUD -= gravity;
}
moveFB = Input.GetAxis("Vertical") * speed;
moveLR = Input.GetAxis("Horizontal") * speed;
rotX = Input.GetAxis("Mouse X") * sensitivity;
rotY = Input.GetAxis("Mouse Y") * sensitivity;
Vector3 Movement = new Vector3(moveLR, moveUD, moveFB);
Movement = transform.rotation * Movement;
transform.Rotate(0, rotX, 0);
eyes.transform.Rotate(-rotY, 0, 0);
player.Move(Movement * Time.deltaTime);
}
I hope this helps! :)
Your answer
Follow this Question
Related Questions
Make the Character Controller jump to the side 1 Answer
Strange Character Controller Behavior Caused by Simulated Gravity and Ground Check 0 Answers
My player keeps launching into the sky 1 Answer
Dampening Movement while in the Air 0 Answers
Moving AI away or towards player not working correctly. 1 Answer