VR穿戴设备 - Pico - 输入系统 - 抛物线

手柄抛物线轨迹判断

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; // 将手榴弹附着到手柄上
        }
    }
}

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值