Unity: 3D movement/Collision检测失败(AddForce, MovePosition, transform.localPosition)
Unity: 3D movement/Collision detection failure (AddForce, MovePosition, transform.localPosition)
问题:
如果我让运动正常工作,则不会检测到碰撞网格。如果我检测到碰撞网格,则运动无法正常工作。
项目小结:
我有一个 3D 环境,其中包含不可移动的对象(带有对撞机网格)和一个可移动的游戏对象(带有 x2 盒式对撞机的刚体),我通过 C++ 应用程序中的 UDP 连接使用触觉设备(基本上是 3D 操纵杆)进行控制在 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 并允许对拖动和我的力修饰符变量进行键控调整,以防其他初学者也看到这个线程。与此同时,我将尝试让其他函数(MovePosition 等)工作并更新结果。
苗条,基本drag/variable 测试代码:
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
的位置与真实物体之间的距离成线性关系,因此这基本上表现得像 spring。您应该尝试将力乘以不同的因子并测试:
rb.AddForce (forceDirection * 0.5f);
或二次缩放:
rb.AddForce (forceDirection * forceDirection.magnitude);
任何感觉最好的
问题: 如果我让运动正常工作,则不会检测到碰撞网格。如果我检测到碰撞网格,则运动无法正常工作。
项目小结: 我有一个 3D 环境,其中包含不可移动的对象(带有对撞机网格)和一个可移动的游戏对象(带有 x2 盒式对撞机的刚体),我通过 C++ 应用程序中的 UDP 连接使用触觉设备(基本上是 3D 操纵杆)进行控制在 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 并允许对拖动和我的力修饰符变量进行键控调整,以防其他初学者也看到这个线程。与此同时,我将尝试让其他函数(MovePosition 等)工作并更新结果。
苗条,基本drag/variable 测试代码:
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
的位置与真实物体之间的距离成线性关系,因此这基本上表现得像 spring。您应该尝试将力乘以不同的因子并测试:
rb.AddForce (forceDirection * 0.5f);
或二次缩放:
rb.AddForce (forceDirection * forceDirection.magnitude);
任何感觉最好的