- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
问题:如果我让运动正常工作,则不会检测到碰撞网格。如果我检测到碰撞网格,则运动无法正常工作。
项目简介:我有一个 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 等)工作并更新结果。
精简、基本的拖动/变量测试代码:
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
放置在 Controller 的确切位置,不如尝试在您希望 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);
感觉最好的
关于c# - unity : 3D movement/Collision detection failure (AddForce, MovePosition, transform.localPosition),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55631027/
Cesium SandcaSTLe demo说明了使用“movement”和“movement.endPosition”来获取鼠标移动的 Cartesian2 坐标。一个例子是 "Picking" d
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我一直在玩 webGL,我已经达到了可以制作图形非常可怜的小型三维游戏的程度(到目前为止,它更多的是概念/功能的证明)。对于三维体验来说,向任意方向无限移动鼠标来旋转第一人称视角是很不错的。 Poin
这是我第一次在这里写文章。我对 JQuery 动画有一些问题:当持续时间非常慢(例如 10000)时, Action 不够平滑。我的 div 需要一个“自然”的运动。有人知道我的问题的解决方案吗?或者
我正在开发离线 FPS 多人游戏。 当 Player Rotation 值为 (0,0,0) 时,Player 会朝着完美的方向移动。但是,我的问题是当我使用触摸输入旋转相机时。玩家也可以旋转脸部并按
我在分布式系统中遇到分片移动问题。 【问题】 最初每个分区负责任意数量的分片。 (这个数字可以是任意的,因为系统支持将分片从一个分区移动到另一个分区) 然后一个新的分区来了,系统需要重新分片。目标是使
是否可以在窗口中停止鼠标移动并仅允许在特定框中移动? 例如,我有一个div,其宽度:200px和高度:200px,只允许在此div中移动鼠标。 谢谢。 最佳答案 Javascript 可以读取鼠标位置
我创建了一个自定义组件,它扩展了 TextField 并允许对特定类型的字符串进行高级编辑。 箭头键已被重新定义以允许特定行为并且应该不会触发默认的插入符移动但是我似乎无法阻止它移动。我已经使用了()
我创建了一个自定义组件,它扩展了 TextField 并允许对特定类型的字符串进行高级编辑。 箭头键已被重新定义以允许特定行为并且应该不会触发默认的插入符移动但是我似乎无法阻止它移动。我已经使用了()
我想做一个函数,根据传递的字母改变特定的值。 (这些基本上给出了方向:东,西......) 代码是: data Movement = N Int | S Int | E Int | W Int de
我正在从事一个非常酷的项目,我正在收集有关光标移动的数据,但我遇到了一个问题,我认为我需要一些帮助。我不断读取有关光标 x 和 y 位置的数据(以及其他相关数据),一旦光标在 y 维度上超过某个阈值,
我怎样才能在 Sprite 被施加这样的冲动后停止它: player.physicsBody!.applyImpulse(CGVectorMake(50, 0)) 是否有可能使运动在一段时间内减少?
我有一个子菜单,当悬停在导航元素上时,它会变为可见。当该元素不再悬停时,子菜单将变为不可见并向上移动。我已经在下面附上了我的部分代码。我面临的问题是,由于子菜单就在导航元素的正下方,向上移动会在转换期
我是 Unity 新手,正在关注 this tutorial .当我尝试执行代码时遇到的错误应该是左右移动播放器是: UnassignedReferenceException: The variabl
谷歌最近在那里重新设计了移动行为 map 。现在在移动设备上,您可以用两根手指移动 map 。 (请参阅手机上的 map-simple 示例,而不是任何浏览器模拟器!)。 我想在 openlayer
据我了解,GPS 的精度有限。是否有可能在 3D 空间中获得一个人的更精确的位置? 我计划编写一个应用程序,用户可以将手机向任何方向移动,但只需要移动几英尺。是否有任何技术(也许使用加速度计和陀螺仪)
如何防止图像移动。如果图像的位置 = 350,如何防止移动。类似于遏制,恰恰相反。请帮忙。 $('#image').draggable( {
这是我应该问的问题,而不是这个: Emacs evil: space as a prefix key in motion state 我想定义一堆用于移动、移动、打开和关闭窗口和缓冲区的命令,这些命令
我是计算机类三人组的三分之一(高中新生)。对于我们的项目,我们决定用 Java 制作一个吃 bean 人风格的游戏。我们都没有太多经验,所以到目前为止,我们基本上都是在自学。我们已经取得了相当大的进步
我正在开发一个类似 HTML5 3D fps 的引擎,它看起来已经相当不错,但由于这可能是制作 3D 的最糟糕的语言选择之一,因此有时会出现明显的延迟。 我将运动 (WASD) 编程为独立于渲染速度,
我是一名优秀的程序员,十分优秀!