using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
[SerializeField] float moveSpeed = 5f;
[SerializeField] float jumpForce = 7f;
[SerializeField] Transform groundCheck;
[SerializeField] float groundCheckRadius = 0.3f;
[SerializeField] LayerMask groundLayer;
[SerializeField] bool move3d = false;
private Rigidbody rb;
private bool isGrounded;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
// Проверка, на земле ли игрок
isGrounded = Physics.CheckSphere(groundCheck.position, groundCheckRadius, groundLayer);
// Получаем ввод по горизонтали (вдоль X) и вперёд/назад (вдоль Z)
float horizontal = Input.GetAxis("Horizontal");
float vertical = 0;
if (move3d == true)
{
// Получаем ввод по вертикали (вдоль X) и вперёд/назад (вдоль Z)
vertical = Input.GetAxis("Vertical");
}
// Создаём вектор движения
Vector3 move = new Vector3(horizontal, vertical).normalized;
// Устанавливаем горизонтальную скорость, сохраняем вертикальную
Vector3 velocity = move * moveSpeed;
velocity.y = rb.velocity.y;
rb.velocity = velocity;
// Прыжок
Debug.Log(isGrounded);
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
rb.velocity = new Vector3(rb.velocity.x, jumpForce, rb.velocity.z);
}
}
private void OnCollisionStay(Collision collision)
{
if (collision.gameObject.GetComponent<ButtonController>() != null && Input.GetKeyDown(KeyCode.E))
{
collision.gameObject.GetComponent<ButtonController>().ButtonPress();
}
}
// Чтобы визуально видеть зону проверки земли
void OnDrawGizmosSelected()
{
if (groundCheck != null)
{
Gizmos.color = Color.green;
Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius);
}
}
}