手柄抛物线轨迹判断
using UnityEngine;
using Pvr_UnitySDKAPI;
public class ParabolaDetection : MonoBehaviour
{
public float parabolaDetectionThreshold = 0.5f; // 抛物线检测的阈值
public float parabolaTimeThreshold = 0.5f; // 抛物线的时间阈值
public LayerMask parabolaLayer; // 抛物线的检测层
private bool isThrowing = false;
private Vector3 initialPosition;
private float throwStartTime;
void Update()
{
// 检测手柄挥动抛物线的动作
if (Pvr_ControllerManager.GetControllerDeviceStatus(0) == Pvr_UnitySDKAPI.ControllerDevice.Connected &&
Pvr_ControllerManager.GetControllerButtonState(0, Pvr_UnitySDKAPI.Pvr_KeyCode.TRIGGER) == Pvr_UnitySDKAPI.Pvr_KeyEvent.UP)
{
if (!isThrowing)
{
initialPosition = Pvr_ControllerManager.controllerlink0.transform.position;
throwStartTime = Time.time;
isThrowing = true;
}
}
// 在抛物线动作中检测抛物线
if (isThrowing)
{
DetectParabola();
}
}
void DetectParabola()
{
Vector3 throwDirection = Pvr_ControllerManager.controllerlink0.transform.position - initialPosition;
float throwSpeed = Vector3.Distance(Pvr_ControllerManager.controllerlink0.transform.position, initialPosition) / (Time.time - throwStartTime);
// 如果手柄速度超过阈值,认为手柄被挥出
if (throwSpeed > parabolaDetectionThreshold)
{
float elapsedTime = Time.time - throwStartTime;
// 在一定时间内检测手柄位置是否符合抛物线
if (elapsedTime <= parabolaTimeThreshold)
{
Ray ray = new Ray(initialPosition, throwDirection);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, Mathf.Infinity, parabolaLayer))
{
// 在这里处理抛物线击中的逻辑
Debug.Log("Parabola hit something at: " + hit.point);
}
}
// 重置抛物线状态
isThrowing = false;
}
}
}
拾取手榴弹并抛出
using UnityEngine;
using Pvr_UnitySDKAPI;
public class GrenadeThrow : MonoBehaviour
{
public GameObject grenadePrefab; // 手榴弹预制体
public float throwForce = 10.0f; // 扔出手榴弹的力度
public float parabolaDetectionThreshold = 0.5f; // 抛物线检测的阈值
public float parabolaTimeThreshold = 0.5f; // 抛物线的时间阈值
public LayerMask parabolaLayer; // 抛物线的检测层
public float pickUpRadius = 1.5f; // 拾取手榴弹的半径
private bool isThrowing = false;
private bool isGrenadePickedUp = false;
private GameObject pickedUpGrenade;
private Vector3 initialPosition;
private float throwStartTime;
void Update()
{
// 检测手柄挥动抛物线的动作
if (Pvr_ControllerManager.GetControllerDeviceStatus(0) == Pvr_UnitySDKAPI.ControllerDevice.Connected &&
Pvr_ControllerManager.GetControllerButtonState(0, Pvr_UnitySDKAPI.Pvr_KeyCode.TRIGGER) == Pvr_UnitySDKAPI.Pvr_KeyEvent.UP)
{
if (!isThrowing && isGrenadePickedUp)
{
initialPosition = Pvr_ControllerManager.controllerlink0.transform.position;
throwStartTime = Time.time;
isThrowing = true;
}
}
// 在抛物线动作中检测抛物线
if (isThrowing)
{
DetectParabola();
}
}
void DetectParabola()
{
Vector3 throwDirection = Pvr_ControllerManager.controllerlink0.transform.position - initialPosition;
float throwSpeed = Vector3.Distance(Pvr_ControllerManager.controllerlink0.transform.position, initialPosition) / (Time.time - throwStartTime);
// 如果手柄速度超过阈值,认为手柄被挥出
if (throwSpeed > parabolaDetectionThreshold)
{
float elapsedTime = Time.time - throwStartTime;
// 在一定时间内检测手柄位置是否符合抛物线
if (elapsedTime <= parabolaTimeThreshold)
{
Ray ray = new Ray(initialPosition, throwDirection);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, Mathf.Infinity, parabolaLayer))
{
// 抛物线击中点
Vector3 hitPoint = hit.point;
// 扔出手榴弹
ThrowGrenade(hitPoint);
}
}
// 重置抛物线状态
isThrowing = false;
}
}
void ThrowGrenade(Vector3 targetPosition)
{
// 手榴弹扔出后,重置拾取状态
isGrenadePickedUp = false;
// 实例化手榴弹并将其扔向目标位置
pickedUpGrenade.transform.parent = null;
Rigidbody grenadeRigidbody = pickedUpGrenade.GetComponent<Rigidbody>();
Vector3 throwDirection = (targetPosition - transform.position).normalized;
grenadeRigidbody.AddForce(throwDirection * throwForce, ForceMode.Impulse);
}
void OnTriggerEnter(Collider other)
{
// 拾取手榴弹
if (other.CompareTag("Grenade"))
{
isGrenadePickedUp = true;
pickedUpGrenade = other.gameObject;
pickedUpGrenade.transform.parent = transform; // 将手榴弹附着到手柄上
}
}
}