0

Есть игрок Player. Вот его Inspector: введите сюда описание изображения

Вот его код:

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

public class ThirdPersonMovement : MonoBehaviour
{
    public CharacterController controller;
    public Transform cam;

    public float speed = 6f;
    public float gravity = -9.81f;

    Vector3 velocity;

    public float turnSmoothTime = 0.1f;
    float turnSmoothVelocity;

    void Update()
    {
        float horizontal = Input.GetAxisRaw("Horizontal");
        float vertical = Input.GetAxisRaw("Vertical");
        Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;

        if (direction.magnitude >= 0.1f)
        {
            float targetAngel = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
            float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngel, ref turnSmoothVelocity, turnSmoothTime);
            transform.rotation = Quaternion.Euler(0f, angle, 0f);

            Vector3 moveDir = Quaternion.Euler(0f, targetAngel, 0f) * Vector3.forward;

            controller.Move(moveDir.normalized * speed * Time.deltaTime);


        }
        velocity.y += gravity * Time.deltaTime;

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

По идее когда я добавил в код эти 2 строчки:

velocity.y += gravity * Time.deltaTime;

controller.Move(velocity * Time.deltaTime);

Он должен начать падать. Но он не падает. Код для Third Person Camera и передвижение игрока я взял из этого видео: https://www.youtube.com/watch?v=4HpC--2iowE

А вот код с гравитацией:

velocity.y += gravity * Time.deltaTime;

controller.Move(velocity * Time.deltaTime);

Я взял из этого видео: https://www.youtube.com/watch?v=_QajrabyTJc

Возможно нужно использовать controller.Move() 2 раза, пробовал но не получилось. Подскажите, как решить эту проблему?

1
  • А с каким целями конкретно ты хочешь писать свою реализацию гравитации вместо использования rigitbody? 13 июл 2021 в 20:46

1 ответ 1

0

Попробуй добавить:

void Start()
{
    velocity = new Vector3(0.0f, 0.0f, 0.0f);
}

Возможно поможет, но это не точно

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.