@loliluv

Персонаж не прыгает в unity3D! Почему?

как бы я не пытался сделать прыжок персонаж ни в какую не хочет прыгать, в право в лево двигается а прыгать никак. если что в input mannager на jump все стоит как нужно я проверял

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

public class PlayerController : MonoBehaviour

{
public float speed = 10f;
public CharacterController controller;
public Transform groundcheck;
public float gravity;
public LayerMask groundMask;
Vector3 velocity;
public bool isgrounded;
public float groundDistance = 0.4f;
public float jumpHeight = 3f;

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);

    velocity.y = gravity * Time.deltaTime;
    controller.Move(velocity * Time.deltaTime);

    if (Input.GetButtonDown("Jump") && isgrounded)
    {
        velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
    }

}
}


65ec43ed1dec4194804363.png
  • Вопрос задан
  • 143 просмотра
Решения вопроса 1
Kentavr16
@Kentavr16
long cold winter
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour

{
    public float speed = 10f;
    public CharacterController controller;
    public Transform groundcheck;
    public float gravity;
    public LayerMask groundMask;
    Vector3 velocity;
    public bool isgrounded;
    public float groundDistance = 0.4f;
    public float jumpHeight = 3f;

    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);

        velocity.y += gravity * Time.deltaTime;
        controller.Move(velocity * Time.deltaTime);

        if (Input.GetButtonDown("Jump") && isgrounded)
        {
            velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
        }

    }
}

что я и говорил - ты переписывал велосити. В этой вариации гравитаця - отрицательная величина. Все работает.
Ответ написан
Пригласить эксперта
Ответы на вопрос 1
AshBlade
@AshBlade Куратор тега C#
Просто хочу быть счастливым
Вместо Input.GetButtonDown("Jump") используй Input.GetKeyDown(KeyCode.Space)
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы