c# - Unity:3D 运动/碰撞检测失败(AddForce、MovePosition、transform.localPosition)
问题描述
问题: 如果我让运动正常工作,则不会检测到碰撞网格。如果我检测到碰撞网格,则运动无法正常工作。
项目简要总结: 我有一个 3D 环境,其中包含不可移动对象(带有碰撞器网格)和一个可移动的游戏对象(带有 x2 盒碰撞器的刚体),我使用触觉设备(基本上是 3D 操纵杆)通过 UDP 连接进行控制一个我放在一起的 C++ 应用程序,它在 Unity 应用程序运行时正在运行。触觉设备和 Unity 之间的通信非常好。我正在使用从触觉设备传递的位置信息作为移动游戏对象的变量。同样,位置数据到达 Unity 就好了;在 Unity 中使用具有适当条件和功能的位置数据的方法是我目前遇到的问题。
我尝试过的事情: 如果我使用transform.localPosition (hapticDevicePosition); 然后运动很棒,但是它忽略了对撞机并穿过了一切。我在线阅读并了解 transform.localPosition 基本上会将我的对象移动到其他对象之上,而不考虑物理。我还读到我可以在我的对象前面引入一条类似于 0.000001 的射线,这样如果射线与任何其他对象交互,它就可以防止移动。这可能是一种仍然能够使用 transform.localPosition 的方法?我不确定,而且我从未使用过光线,因此我很难正确设置该脚本。
我试过AddForce。这表现得很奇怪。它只给我 2 个力输出而不是 3 个......即,我只能在 3 个轴中的 2 个中移动。我不明白为什么它会这样。然而,碰撞体被检测到。
我试过rb.MovePosition (rb.position + posX + posY + posZ) 以及 *Time.timeDelay 和 *speed 的各种组合。这也不能正常工作。检测到碰撞器,但运动要么根本不起作用,要么不能正常工作。
结论: 在过去的 4 个小时里,我一直在使用我的脚本,并且我尝试的一些(不是全部)东西被注释掉了,所以它们仍然可见(请参阅下面的代码)。如果我找到解决方案,我将阅读更多在线解释并尝试不同的代码并在此处更新。同时,如果有人有一些指示或建议,我将不胜感激。
谢谢!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FalconPegControl_2 : MonoBehaviour {
// Define needed variables
private TestUDPConnection udpListener;
public Vector3 realObjectCurrentPos;
private Vector3 realObjectLastPos;
public Vector3 realObjectCurrentRot;
private Vector3 realObjectLastRot;
public Vector3 realObjectPosChange;
public Vector3 realObjectRotChange;
private Quaternion rotation;
//public float pi = 3.14f;
private Rigidbody rb;
private int control = 0;
public bool collisionOccurred = false;
//public float thrust = 1000;
//public CalibrationManager calibrationManager;
// Use this for initialization
void Start () {
udpListener = GetComponentInParent<TestUDPConnection>();
collisionOccurred = false;
rb = GetComponent<Rigidbody> ();
SharedRefs.falconPegControl = this;
}
public void OffControl ()
{
control = 0;
}
public void CollisionDuplicateFix ()
{
collisionOccurred = true;
}
// Update is called once per frame
void FixedUpdate () {
//WITHOUT UNITY AXIS CONVERSION:
//realObjectCurrentPos[0] = udpListener.xPosReal; //[m]
//realObjectCurrentPos[1] = udpListener.yPosReal; //[m]
//realObjectCurrentPos[2] = udpListener.zPosReal; //[m]
//===============================
//Unity axis conversions:
//CHAI3D --> Unity
//(x, y, z) --> (x, -z, y)
//CHAI3D: realObjectCurrentPos[0], [1], [2] is CHIA3D (x, y, z)
//Also, to compensate for the workspace available to the Falcon Device (~0.04, ~0.06, ~0.06)
//adding a value of x10 allows it to reach the default hemisphere successfully
//updated comment: the sign values that work (-, +, -)
//===============================
//Unity conversion for rotation (using Falcon devices)
//Since one falcon is for translation and the other is for rotation,
//the rotation information is a conversion of translational information
//in other words, max range of (~0.04, ~0.06, ~0.06) has been converted into a max range of (90, 90, 90)
//using basic algebra (i.e., (90/0.04))
//thus giving the user the full range of 180 degrees (from 90 degrees to -90 degrees)
realObjectCurrentPos[0] = udpListener.xPosReal * (-5); //[m]
realObjectCurrentPos[1] = udpListener.zPosReal * (5); //[m]
realObjectCurrentPos[2] = udpListener.yPosReal * (-5); //[m]
realObjectCurrentRot [0] = udpListener.xRot * (90f / 0.04f); //degrees
realObjectCurrentRot [1] = udpListener.yRot * (90f / 0.06f); //degrees
realObjectCurrentRot [2] = udpListener.zRot * (90f / 0.06f); //degrees
if (Input.GetKeyDown ("1")) {
control = 1;
SharedRefs.stopWatch.startTimer ();
}
if (Input.GetKeyDown ("space"))
{
OffControl ();
}
if (control==1)
{
Vector3 posUnity = new Vector3 (realObjectCurrentPos[0], realObjectCurrentPos[1], realObjectCurrentPos[2]);
rb.AddForce (posUnity);
//Vector3 tempVect = new Vector3(realObjectCurrentPos[0], realObjectCurrentPos[1], realObjectCurrentPos[2]);
//Vector3 startPoint = new Vector3 (0f, 0.0225f, 0f);
//tempVect = tempVect * speed * Time.deltaTime;
//transform.localPosition = realObjectCurrentPos; //[m]
//var unityX = Vector3.Scale (posTemp, Vector3.right);
//var unityY = Vector3.Scale (posTemp, Vector3.up);
//var unityZ = Vector3.Scale (posTemp, Vector3.forward);
//Vector3 unityX = new Vector3 (Vector3.Scale (posTemp, Vector3.right), Vector3.Scale (posTemp, Vector3.up), Vector3.Scale (posTemp, Vector3.forward));
//Vector3 unityY = new Vector3 (Vector3.Scale (posTemp, Vector3.up));
//Vector3 unityZ = new Vector3 (Vector3.Scale (posTemp, Vector3.forward));
//rb.MovePosition (rb.position + unityX + unityY + unityZ);
//transform.localPosition = (startPoint + tempVect); //[m]
transform.localRotation = Quaternion.Euler(realObjectCurrentRot); //[m]
realObjectLastPos = realObjectCurrentPos;//[m]
realObjectLastRot = realObjectCurrentRot;//[m]
realObjectPosChange = realObjectCurrentPos - realObjectLastPos; //[m]
realObjectRotChange = realObjectCurrentRot - realObjectLastRot;
}
else if (control==0)
{
Vector3 stop = new Vector3 (0, 0, 0);
rb.constraints = RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotationZ;
rb.constraints = RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezeRotationX;
rb.constraints = RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezeRotationX;
rb.velocity = (stop);
}
}
}
此外,根据@Ali Baba 的评论更新:我还没有时间测试其他方法,但是通过使用 AddForce 并使用拖动和力修改变量,我能够控制所有三个轴(实际上是 6DOF因为我还可以通过第二个外部设备进行旋转控制),而且我对我的游戏对象的控制也比以前更好(特别是由于拖动和力修改器变量的调整)。这可能是最好的解决方案,但我最初需要根据我正在使用的外部设备的位置来改变我的位置。我正在添加一个基本的、精简的、调整过的代码,它使用 AddForce 并允许对拖动和我的力修饰符变量进行 keycontrol 调整,以防其他初学者也看到这个线程。同时,
苗条,基本的拖动/变量测试代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Real_Controller : MonoBehaviour {
// Define needed variables
private TestUDPConnection udpListener;
public Vector3 realObjectCurrentPos;
public Vector3 realObjectCurrentRot;
private Quaternion rotation;
private Rigidbody rb;
private float increaseForce = 23;
// Use this for initialization
void Start () {
udpListener = GetComponentInParent<TestUDPConnection>();
rb = GetComponent<Rigidbody> ();
rb.drag = 1.24f;
}
// Update is called once per frame
void FixedUpdate () {
if (Input.GetKeyDown ("q"))
{
rb.drag -= 0.1f;
Debug.Log ("drag is: " + rb.drag);
}
if (Input.GetKeyDown ("w"))
{
rb.drag += 0.1f;
Debug.Log ("drag is: " + rb.drag);
}
if (Input.GetKeyDown ("a")) {
increaseForce -= 1f;
Debug.Log ("increased force is: " + increaseForce);
}
if (Input.GetKeyDown ("s")) {
increaseForce += 1f;
Debug.Log ("increase force is: " + increaseForce);
}
realObjectCurrentPos[0] = udpListener.xPosReal * (-increaseForce); //[m]
realObjectCurrentPos[1] = udpListener.zPosReal * (increaseForce); //[m]
realObjectCurrentPos[2] = udpListener.yPosReal * (-increaseForce); //[m]
Vector3 forceDirection = realObjectCurrentPos - transform.localPosition;
rb.AddForce (forceDirection * forceDirection.magnitude);
realObjectCurrentRot [0] = udpListener.xRot * (90f / 0.04f); //degrees
realObjectCurrentRot [1] = udpListener.yRot * (90f / 0.06f); //degrees
realObjectCurrentRot [2] = udpListener.zRot * (90f / 0.06f); //degrees
transform.localRotation = Quaternion.Euler(realObjectCurrentRot); //[m]
}
}
解决方案
gameObject
您可以尝试在您希望所在位置的方向上施加力,而不是将其放置在控制器的确切位置gameObject
:
if (control==1)
{
Vector3 forceDirection = realObjectCurrentPos - transform.localPosition;
rb.AddForce (forceDirection);
transform.localRotation = Quaternion.Euler(realObjectCurrentRot)
}
gameObject
此处施加的力与真实物体的位置之间的距离成线性关系,因此这基本上就像弹簧一样。您应该尝试将力乘以不同的因素并测试:
rb.AddForce (forceDirection * 0.5f);
或二次缩放:
rb.AddForce (forceDirection * forceDirection.magnitude);
什么感觉最好
推荐阅读
- java - 缺少 Java 清单属性
- django - django 在 select_related 字段上进行注释
- php - 编写 php 文件以将文档添加到 mongoDB 集合
- javascript - 在父组件相同的两个组件之间发送数据
- networking - 升级到 Debian 9 后,具有多个 IP 的容器(LXC)的网络配置不起作用
- deep-learning - 为什么当论文'Attention is all you need'中的值很大时softmax会得到小的梯度
- node.js - 我可以使用 mongodb ref 来引用同一模型中的模型吗
- botframework - 使用 Cortana 的聊天机器人不会说话
- javascript - 聚合物 3,模板中的槽元素未填充
- c++ - 有什么方法可以安全地处理通过 delete-expression 释放内存的两次?