攻撃できるようになりました
This commit is contained in:
44
Assets/Scripts/EnemyHealth.cs
Normal file
44
Assets/Scripts/EnemyHealth.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
public class EnemyHealth : MonoBehaviour, IDamageble
|
||||
{
|
||||
[SerializeField] private float maxHealth = 100f;
|
||||
private float currentHealth;
|
||||
|
||||
[SerializeField] private float knockbackForce = 5f;
|
||||
private Rigidbody rb;
|
||||
|
||||
void Start()
|
||||
{
|
||||
currentHealth = maxHealth; //HPを代入
|
||||
rb = GetComponent<Rigidbody>();
|
||||
}
|
||||
|
||||
public void TakeDamage(DamageInfo damageInfo)
|
||||
{
|
||||
if(currentHealth <= 0) return;
|
||||
|
||||
currentHealth -= damageInfo.amount; //ダメージ計算
|
||||
|
||||
if(damageInfo.isPoseBonus)
|
||||
{
|
||||
Debug.Log("構えボーナス");
|
||||
}
|
||||
|
||||
if(rb != null) //ノックバック
|
||||
{
|
||||
rb.AddForce(damageInfo.punchDirection * knockbackForce, ForceMode.Impulse); //パンチが飛んできた方向に力を加える
|
||||
}
|
||||
|
||||
if(currentHealth <= 0)
|
||||
{
|
||||
Die();
|
||||
}
|
||||
}
|
||||
|
||||
private void Die()
|
||||
{
|
||||
Destroy(gameObject, 0.5f);
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/EnemyHealth.cs.meta
Normal file
2
Assets/Scripts/EnemyHealth.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4b4f072c824585843b120965516325b6
|
||||
13
Assets/Scripts/IDamageble.cs
Normal file
13
Assets/Scripts/IDamageble.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using UnityEngine;
|
||||
|
||||
public struct DamageInfo
|
||||
{
|
||||
public float amount; //ダメージ量
|
||||
public Vector3 hitPosition; //殴られた位置
|
||||
public Vector3 punchDirection; //パンチが飛んできた方向(ノックバック用)
|
||||
public bool isPoseBonus; //構えから攻撃かどうか
|
||||
}
|
||||
public interface IDamageble
|
||||
{
|
||||
void TakeDamage(DamageInfo damageInfo);
|
||||
}
|
||||
2
Assets/Scripts/IDamageble.cs.meta
Normal file
2
Assets/Scripts/IDamageble.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 42a222f4862dd2c46846a6be4dfe956a
|
||||
84
Assets/Scripts/VRPunchAttack.cs
Normal file
84
Assets/Scripts/VRPunchAttack.cs
Normal file
@@ -0,0 +1,84 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem; // Input Systemを使うために必要
|
||||
using UnityEngine.XR; // 振動機能のために必要
|
||||
|
||||
[RequireComponent(typeof(Collider))]
|
||||
[RequireComponent(typeof(Rigidbody))]
|
||||
public class VRInputPunch : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private InputActionProperty velocityAction;
|
||||
|
||||
[SerializeField] private XRNode controllerNode = XRNode.RightHand;
|
||||
|
||||
[SerializeField] private float minPunchSpeed = 2.0f; //パンチとして判定する最低速度 (m/s)
|
||||
|
||||
[SerializeField] private float damageMultiplier = 10.0f; //ダメージ倍率
|
||||
|
||||
[SerializeField] private float hapticAmplitude = 0.5f; //振動の強さ
|
||||
[SerializeField] private float hapticDuration = 0.1f; //振動の長さ
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
if (velocityAction.action != null)
|
||||
{
|
||||
velocityAction.action.Enable();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
if (velocityAction.action != null)
|
||||
{
|
||||
velocityAction.action.Disable();
|
||||
}
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
Rigidbody rb = GetComponent<Rigidbody>();
|
||||
rb.isKinematic = true;
|
||||
rb.useGravity = false;
|
||||
GetComponent<Collider>().isTrigger = true;
|
||||
}
|
||||
|
||||
private void OnTriggerEnter(Collider other)
|
||||
{
|
||||
Vector3 velocity = velocityAction.action.ReadValue<Vector3>();
|
||||
|
||||
float speed = velocity.magnitude;
|
||||
|
||||
Debug.Log($"[InputSystem接触] {other.name} に触れました。センサー速度: {speed:F2} m/s");
|
||||
|
||||
if (speed >= minPunchSpeed)
|
||||
{
|
||||
IDamageble damageble = other.GetComponentInParent<IDamageble>();
|
||||
|
||||
if (damageble != null)
|
||||
{
|
||||
DamageInfo info = new DamageInfo();
|
||||
info.amount = speed * damageMultiplier;
|
||||
info.hitPosition = other.transform.position;
|
||||
info.punchDirection = velocity.normalized; //パンチの進む向き
|
||||
info.isPoseBonus = false;
|
||||
damageble.TakeDamage(info);
|
||||
Debug.Log("パンチ成功 ダメージ: {damage:F1}");
|
||||
|
||||
TriggerHaptics();
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("スピードが足りません。必要: {minPunchSpeed} / 現在: {speed:F2}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// コントローラーを振動させる処理
|
||||
private void TriggerHaptics()
|
||||
{
|
||||
UnityEngine.XR.InputDevice device = InputDevices.GetDeviceAtXRNode(controllerNode);
|
||||
if (device.isValid)
|
||||
{
|
||||
device.SendHapticImpulse(0, hapticAmplitude, hapticDuration);
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/VRPunchAttack.cs.meta
Normal file
2
Assets/Scripts/VRPunchAttack.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 432d44ce69f716a499b8e70a63583b9e
|
||||
Reference in New Issue
Block a user