unity3d - 同时从不同的脚本访问刚体
问题描述
我同时使用了两个混合现实控制器。在我的游戏中,左右触发器都做了不同的事情。问题是我一次只能让其中一个访问刚体。如果左侧控制器附加了脚本,它将正常工作。如果正确的控制器附加了脚本,它将正常工作。但我不能让他们两个同时工作。我附上了下面的代码。此脚本在两个控制器上。
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);
}
}
}
解决方案
尝试在输入方向上施加力,而不是设置刚体的速度。这样,两个控制器不会相互覆盖,而是会产生一些组合运动。
推荐阅读
- java - 表单提交一行,但是当添加另一行时,只是得到whitelabel错误页面
- java - 将 HashMap 加载到嵌套配置 bean 会引发绑定异常
- javascript - onClick 操作后打印函数结果
- php - Angular POST 到 PHP:file_put_contents 未写入/错误的提交顺序
- c# - 在 WPF C# 应用程序中执行标准形状实例的深层复制
- android - 使用 Unity 的 IL2CPP ARM64 编译模式在 Android 应用上使用 Vuforia 时出现黑屏
- php - 使用多个 WhereIn 子句 Laravel 4
- oracle-sqldeveloper - 如何修复“ORA-12705:无法访问 NLS 数据文件或指定的环境无效”
- sql - ASP.NET 和 SQL Server 中的动态表单
- angular - Angular 7 - 创建手风琴菜单