问题:如果我使运动正常工作,则不会检测到碰撞网格。如果我检测到碰撞网格,则移动无法正常工作。
项目简介:我有一个带有不可移动物体(带有对撞机网格物体)的3D环境和一个可移动的游戏对象(带有x2箱式对撞机的刚体),我通过UDP连接使用触觉设备(基本上是一个3D操纵杆)来控制我运行的一个C ++应用程序,在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等)工作并更新结果。
超薄,基本的拖动/可变测试代码:
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);
无论什么感觉最好