【发布时间】:2019-01-22 01:22:09
【问题描述】:
我正在尝试将一个立方体从 A 移动到 B。我正在开发的应用程序使用 Unity / ARKit 来检测飞机。在触摸事件中,它会放置一个立方体。这目前按预期工作。
我现在想将放置的立方体转换到一个新位置,但是我的立方体是静止的。在下面的代码中,我放置了立方体。然后,在if 语句中,我调用了一个名为MoveToB 的方法,它应该获取名为objectToPlace 的游戏对象并转换它的位置。我认为这是代码出错的地方。
void Update()
{
UpdatePlacementPose();
UpdatePlacementIndicator();
if (placementPoseIsValid && Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) {
PlaceObject();
Debug.Log("Object Placed"); // This gets called
MoveToB();
}
}
private void MoveToB()
{
objectToPlace.transform.position = new Vector3(100, 100, 100);
Debug.Log("MoveToB Triggered"); // This gets called
}
有人能指出我正确的方向吗?
其余代码...
private void PlaceObject()
{
Instantiate(objectToPlace, placementPose.position, placementPose.rotation);
}
private void UpdatePlacementIndicator()
{
if (placementPoseIsValid) {
placementIndicator.SetActive(true);
placementIndicator.transform.SetPositionAndRotation(placementPose.position, placementPose.rotation);
} else {
placementIndicator.SetActive(false);
}
}
private void UpdatePlacementPose()
{
var screenCentre = Camera.current.ViewportToScreenPoint(new Vector3(0.5f, 0.5f));
var hits = new List<ARRaycastHit>();
arOrigin.Raycast(screenCentre, hits, TrackableType.Planes);
placementPoseIsValid = hits.Count > 0;
if (placementPoseIsValid) {
placementPose = hits[0].pose;
var cameraForward = Camera.current.transform.forward;
var cameraBearing = new Vector3(cameraForward.x, 0, cameraForward.z).normalized;
placementPose.rotation = Quaternion.LookRotation(cameraBearing);
}
}
【问题讨论】:
-
PlaceObject、UpdatePlacementPose和UpdatePlacementIndicator到底发生了什么? -
PlaceObject得到objectToPlace和placementPose的位置和旋转(按预期工作)。UpdatePlacementPose找到有效的平面并将它们存储在一个数组中(按预期工作)。然后它使用 AR 相机进行定位和旋转。UpdatePlacementIndicator根据UpdatePlacementPose更改平面所面对的方向(按预期工作)。 -
objectToPlace是预制件吗?
标签: unity3d