Home > Net >  Jumping dosent work, Unity character controller
Jumping dosent work, Unity character controller

Time:12-14

I am having problems with my jumping script in unity, When i press the jump button nothing happens. My guess is that its the ground check that is making this problem. Every thing else is setup right the ground has the ground layer etc. I have also tried with out the jumping method and the scripts works perfect and i also dont get any errors in unity.

i tried remaking the ground check.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    //Floats

    //Speed Settings
    private float movementSpeed;
    public float normalSpeed = 3f;
    public float sprintSpeed = 5f;
    //Stamina
    public float currentStamina;
    public float staminaUsageMultiplier = 2.5f;
    public float stamina = 100f;
    //Gravity
    public float gravity = -9.8f;
    //Jump Settings
    public float jumpHight = 3f;
    //Inputs
    private float horInput;
    private float verInput;

    //Bools
    private bool isGrounded;

    //Components
    private CharacterController controller;

    //Vectors
    private Vector3 move;
    private Vector3 velocity;

    //Transforms
    public Transform groundCheck;

    //LayerMaskes
    public LayerMask groundLayer;
    private void Awake()
    {
        controller = GetComponent<CharacterController>();
    }
    void Start()
    {
        currentStamina = stamina;
    }

    void Update()
    {
        MovementMethod();
        GravityMethod();
        JumpMethod();
    }

    void MovementMethod()
    {
        horInput = Input.GetAxis("Horizontal");
        verInput = Input.GetAxis("Vertical");

        move = transform.forward * verInput   transform.right * horInput;

        controller.Move(move * movementSpeed * Time.deltaTime);

        if(Input.GetKey(KeyCode.LeftShift) && stamina != 0)
        {
            movementSpeed = sprintSpeed;

            currentStamina -= staminaUsageMultiplier * Time.deltaTime;
        }
        else
        {
            movementSpeed = normalSpeed;

        }
    }

    void GravityMethod()
    {
        isGrounded = Physics.CheckSphere(groundCheck.position, 0.5f, groundLayer);

        if(isGrounded && velocity.y <0)
        {
            velocity.y = -2f;
        }

        velocity.y  = gravity * Time.deltaTime;

        controller.Move(velocity * Time.deltaTime);
    }

    void JumpMethod()
    {
        if(Input.GetKey(KeyCode.Space) && isGrounded)
        {
            velocity.y = Mathf.Sqrt(jumpHight * -2f * gravity);
        }
    }
}

CodePudding user response:

Check your groundLayer and groundCheckPosition.I guess your gourndLayer is not right or your groundcheck point is too high.You can use Debug.DrawLine to make Phisic.CheckSphere visiable.

CodePudding user response:

Why times gravity? velocity.y = Mathf.Sqrt(jumpHight * -2f * gravity); And also I don't think that 7.74 is enough velocity to get off ground

7.74 because: sqrt(3 * -2 * 9.8)

  • Related