C# Unity:3D移动/碰撞检测失败(AddForce、MovePosition、transform.localPosition)
问题: 如果我使运动正常工作,则不会检测到碰撞网格。如果检测到碰撞网格,则运动无法正常工作 项目摘要: 我有一个3D环境,其中有不可移动物体(带有对撞网格)和一个可移动的游戏对象(刚体与X2盒对撞机),我通过触觉设备(基本上是3D操纵杆)控制,通过一个C++应用程序中的UDP连接,我把它放在一起,在Unity应用程序运行时运行。触觉装置和统一体之间的交流是完美的。我使用触觉设备传递的位置信息作为移动游戏对象的变量。再次,位置数据刚好到达统一位置;在Unity中使用具有适当条件和功能的位置数据的方法是我目前遇到的问题 我尝试过的事情: 如果我使用transform.localPosition(hapticDevicePosition);然后,运动是伟大的,但它忽略了对撞机,并通过一切。我在网上阅读并理解transform.localPosition基本上将我的对象移动到其他对象的顶部,而不考虑物理因素。我还读到,我可能能够在我的对象前面引入一条类似于0.000001的光线,这样,如果光线与任何其他对象交互,它就会阻止移动。这可能是一种仍然能够使用transform.localPosition的方法?我不确定,而且我从未使用过光线,所以我很难正确设置脚本 我试过添加力量。这种行为非常奇怪。它只给我2个力输出,而不是3个…也就是说,我只能在3个轴中的2个轴上移动。我不明白它为什么会这样。然而,对撞机是被检测到的 我尝试了rb.MovePosition(rb.position+posX+posY+posZ)以及*Time.timeDelay和*speed的各种组合。这也不能正常工作。碰撞器被检测到,但运动要么根本不工作,要么工作不正常。C# Unity:3D移动/碰撞检测失败(AddForce、MovePosition、transform.localPosition),c#,unity3d,position,gameobject,rigid-bodies,C#,Unity3d,Position,Gameobject,Rigid Bodies,问题: 如果我使运动正常工作,则不会检测到碰撞网格。如果检测到碰撞网格,则运动无法正常工作 项目摘要: 我有一个3D环境,其中有不可移动物体(带有对撞网格)和一个可移动的游戏对象(刚体与X2盒对撞机),我通过触觉设备(基本上是3D操纵杆)控制,通过一个C++应用程序中的UDP连接,我把它放在一起,在Unity应用程序运行时运行。触觉装置和统一体之间的交流是完美的。我使用触觉设备传递的位置信息作为移动游戏对象的变量。再次,位置数据刚好到达统一位置;在Unity中使用具有适当条件和功能的位置数据的方
结论: 在过去的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);
}
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公营级猎鹰2:单一行为{
//定义所需的变量
专用测试UDP连接udpListener;
公共向量3 realObjectCurrentPos;
私有向量3 realObjectLastPos;
公共向量3 realObjectCurrentRot;
私有向量3 realObjectLastRot;
公共向量3 realObjectPosChange;
公共向量3 realObjectRotChange;
私有四元数旋转;
//公共浮点数pi=3.14f;
私人刚体;
私有int控制=0;
public bool CollisionOccursed=false;
//公共浮动推力=1000;
//公共校准管理器校准管理器;
//用于初始化
无效开始(){
udpListener=getComponentParent();
碰撞发生=错误;
rb=GetComponent();
SharedRefs.falconPegControl=此;
}
公共交通管制()
{
控制=0;
}
公共无效冲突重复修复()
{
碰撞发生=真;
}
//每帧调用一次更新
无效固定更新(){
//没有单位轴转换:
//realObjectCurrentPos[0]=udpListener.xPosReal;//[m]
//realObjectCurrentPos[1]=udpListener.yPosReal;//[m]
//realObjectCurrentPos[2]=udpListener.zPosReal;//[m]
//===============================
//单位轴转换:
//CHAI3D-->统一
//(x,y,z)-->(x,-z,y)
//CHAI3D:realObjectCurrentPos[0]、[1]、[2]是CHIA3D(x,y,z)
//另外,为了补偿Falcon设备可用的工作空间(~0.04、~0.06、~0.06)
//添加值x10可使其成功到达默认半球
//更新注释:有效的符号值(-,+,-)
//===============================
//旋转单位转换(使用Falcon设备)
//由于一只猎鹰用于平移,另一只用于旋转,
//旋转信息是平移信息的转换
//换句话说,最大范围(~0.04、~0.06、~0.06)已转换为最大范围(90、90、90)
//使用基本代数(即(90/0.04))
//从而为用户提供180度的全范围(从90度到-90度)
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);//度
realObjectCurrentRot[1]=udpListener.yRot*(90f/0.06f);//度
realObjectCurrentRot[2]=udpListener.zRot*(90f/0.06f);//度
if(Input.GetKeyDown(“1”)){
对照组=1;
SharedRefs.stopWatch.startTimer();
}
if(Input.GetKeyDown(“空格”))
{
管制主任();;
}
如果(控制==1)
{
Vector3 posUnity=新的Vector3(realObjectCurrentPos[0],realObjectCurrentPos[1],realObjectCurrentPos[2]);
rb.AddForce(posUnity);
//Vector3 tempVect=新Vector3(realObjectCurre
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]
}
}
if (control==1)
{
Vector3 forceDirection = realObjectCurrentPos - transform.localPosition;
rb.AddForce (forceDirection);
transform.localRotation = Quaternion.Euler(realObjectCurrentRot)
}
rb.AddForce (forceDirection * 0.5f);
rb.AddForce (forceDirection * forceDirection.magnitude);