我试图将多维数据集从A移到B。我正在使用的应用程序使用Unity / ARKit来检测平面。在触摸事件中,它将放置一个多维数据集。目前,这可以按预期进行。
我现在想将放置的立方体转换到新位置,但是我的立方体是固定的。在下面的代码中,我放置了多维数据集。然后,在该if
语句中,我调用一个名为MoveToB
的方法,该方法应获取名为objectToPlace
的GameObject并转换其位置。我认为这是代码出问题的地方。
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);
}
}
答案 0 :(得分:0)
objectToPlace
似乎是预制件?
如果不是
objectToPlace.transform.position = new Vector3(100, 100, 100);
尝试翻译该预制件,而不是翻译您在场景中实例化的对象(克隆)。
相反,您应该存储放置对象的引用
private GameObject spawnedObject;
private void PlaceObject()
{
spawnedObject = Instantiate(objectToPlace, placementPose.position, placementPose.rotation);
}
然后移动
private void MoveToB()
{
spawnedObject.transform.position = new Vector3(100, 100, 100);
Debug.Log("MoveToB Triggered"); // This gets called
}