首页 > 解决方案 > 同时从不同的脚本访问刚体

问题描述

我同时使用了两个混合现实控制器。在我的游戏中,左右触发器都做了不同的事情。问题是我一次只能让其中一个访问刚体。如果左侧控制器附加了脚本,它将正常工作。如果正确的控制器附加了脚本,它将正常工作。但我不能让他们两个同时工作。我附上了下面的代码。此脚本在两个控制器上。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR;

public class ControllerRight : MonoBehaviour
{
    public GameObject ballObj;
    Rigidbody rb;

    public SteamVR_Behaviour_Pose pose;
    public SteamVR_Input_Sources handType;
    public SteamVR_Action_Single clickTrigger;
    // Start is called before the first frame update
    void Start()
    {

        rb = ballObj.GetComponent<Rigidbody>();

    }

    // Update is called once per frame
    void FixedUpdate()
    {
        Debug.Log("Trigger Status: " + clickTrigger.GetAxis(handType));
        if (clickTrigger.GetAxis(handType) > 0.5)
        {
            rb.velocity = Vector3.forward * clickTrigger.GetAxis(handType) * 10;
        }
        else if (clickTrigger.GetAxis(handType) == 0)
        {
            rb.velocity = new Vector3(0, 0, 0);
        }
    }

}

标签: unity3dwindows-mixed-reality

解决方案


尝试在输入方向上施加力,而不是设置刚体的速度。这样,两个控制器不会相互覆盖,而是会产生一些组合运动。


推荐阅读