Mail.ruПочтаМой МирОдноклассникиВКонтактеИгрыЗнакомстваНовостиКалендарьОблакоЗаметкиВсе проекты

Помогите пожалуйста персонаж не прыгает unity 3d

Марсель Халимов Ученик (215), открыт 2 недели назад
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Movement : MonoBehaviour
{
[SerializeField] private float _speedWalk;
[SerializeField] private float _gravity;
[SerializeField] private float _jumpPower;
[SerializeField] private float _speedRun;

private CharacterController _characterController;
private Vector3 _walkDirection;
private Vector3 _velocity;
private float _speed;

private void Start()
{
_speed = _speedWalk;
_characterController = GetComponent<CharacterController>();
}

private void Update()
{
Jump(Input.GetKey( KeyCode.Space ) && _characterController.isGrounded);
Run(Input.GetKey(KeyCode.LeftShift));
Sit(Input.GetKey(KeyCode.LeftControl));
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
_walkDirection = transform.right * x + transform.forward * z;
}

private void FixedUpdate()
{
Walk(_walkDirection);
DoGravity(_characterController.isGrounded);
}

private void Walk(Vector3 direction)
{
_characterController.Move(direction * _speedWalk * Time.fixedDeltaTime);
}

private void DoGravity(bool isGrounded)
{
if (isGrounded && _velocity.y < 0);
_velocity.y = -1f;
_velocity.y -= _gravity * Time.fixedDeltaTime;
_characterController.Move(_velocity * Time.fixedDeltaTime);
}

private void Jump(bool canJump)
{
if (canJump)
_velocity.y = _jumpPower;
}

private void Run(bool canRun)
{
_speedWalk = canRun ? _speedRun : _speed;
}

private void Sit(bool canSit)
{
_characterController.height = canSit ? 1f : 2f;
}
}
0 ответов
Похожие вопросы