43 lines
1.4 KiB
C#
43 lines
1.4 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class Player : MonoBehaviour
|
|
{
|
|
private Vector3 _input;
|
|
[Range(1f,100f)][SerializeField] private float _speed;
|
|
[SerializeField] private int _health = 10;
|
|
[SerializeField] private TMPro.TextMeshProUGUI _healthText;
|
|
private bool _isMovementEnabled = true;
|
|
|
|
public bool GetIsMovementEnabled() => _isMovementEnabled;
|
|
public void SetIsMovementEnabled(bool isMovementEnabled) => _isMovementEnabled = isMovementEnabled;
|
|
public int GetHealth() => _health;
|
|
|
|
public void AddHealth(int health)
|
|
{
|
|
if (_health <= 0)
|
|
{
|
|
return;
|
|
}
|
|
_health += health;
|
|
_healthText.text = _health.ToString();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (_isMovementEnabled)
|
|
{
|
|
_input = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical"));
|
|
if (Input.GetAxisRaw("Vertical") > 0)
|
|
{
|
|
gameObject.transform.Translate(Vector3.forward * (_speed * Time.deltaTime));
|
|
}else if (Input.GetAxisRaw("Vertical") < 0)
|
|
{
|
|
gameObject.transform.Translate(Vector3.back * (_speed * Time.deltaTime));
|
|
}
|
|
gameObject.transform.Rotate(0, Input.GetAxisRaw("Horizontal") * 100f * Time.deltaTime, 0);
|
|
}
|
|
}
|
|
}
|