Reputation: 142
I am following an FPS tutorial here. Unfortunately, at the end when Brackeys tests out the code, I cannot jump. Here is my player movement script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;
public float speed = 10f;
public float gravity = -10f;
public float jumpHeight = 5f;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
Vector3 velocity;
bool isGrounded;
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
if (Input.GetButton("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
Here is the screenshot for the code component in the unity editor:
Please help me! Also, if you need any clarification, do not hesitate to ask!
Upvotes: 1
Views: 570
Reputation: 31
No need for ground check from Brackeys's FPS code since the character controller has its public isGrounded property.
Upvotes: 0
Reputation: 444
I have some suggestions for your problem but I am not 100% sure that they will work.
Ground
is attached to the gameobject that is your platform. If you don't know how to do this, check this doc on layers: https://docs.unity3d.com/Manual/Layers.htmlI believe these will work. If there are still any problems, comment down below.
Upvotes: 3
Reputation: 31
I haven't tried the code myself yet, but if you increase the Jumpheight to something like 300 the player should jump up.
This is because your formula:
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
Ends up being smaller than the gravity , so you don't have enough force to jump up.
Upvotes: 1