我在Unity上使用ARCore,试图将一个gameObject放在两个gameObject之间。不幸的是,它没有显示,似乎是Anchor的问题。
*使用Vuforia,我可以轻松地放置游戏对象。
我想要实现的是:
在两个对象(A和B)之间放置一个游戏对象(C)。不显示
// Place target objects A and B on surface touch
GameObject prefab;
GameObject prefab2;
prefab = AndyPointPrefab;
prefab2 = AndyPlanePrefab;
hitpoint= hitpoint + 1;
// Place object A
if (hitpoint==1){
target1 = Instantiate(prefab, hit.Pose.position, hit.Pose.rotation);
target1.transform.Rotate(0, k_ModelRotation, 0, Space.Self);
var anchor = hit.Trackable.CreateAnchor(hit.Pose);
target1.transform.parent = anchor.transform;
mIsFirsttargetVisible=true;
}
else if (hitpoint==2){ // Place object B
target2 = Instantiate(prefab2, hit.Pose.position, hit.Pose.rotation);
target2.transform.Rotate(0, k_ModelRotation, 0, Space.Self);
var anchor = hit.Trackable.CreateAnchor(hit.Pose);
target2.transform.parent = anchor.transform;
mIsSecondTargetVisible=true;
hitpoint=-1;
}
//Update function : todisplay point C in-between.
Update() {
// Place object C
if(mIsFirsttargetVisible && mIsSecondTargetVisible){
GameObject objectMiddle = GameObject.CreatePrimitive(PrimitiveType.Sphere);
Vector3 middlePosition = (target1.transform.position+target2.transform.position)/2;
objectMiddle.transform.position = middlePosition;
objectMiddle.transform.localScale = new Vector3(200.0f, 10.0f, 200.0f);
}
}
编辑
我成功到达了将游戏对象放置在两者之间的位置。
我在代码中添加了最后一行:target1.transform.parent = objectMiddle.transform;
if(mIsFirsttargetVisible && mIsSecondTargetVisible){
GameObject objectMiddle = GameObject.CreatePrimitive(PrimitiveType.Sphere);
Vector3 middlePosition = (target1.transform.position+target2.transform.position)/2;
objectMiddle.transform.position = middlePosition;
objectMiddle.transform.localScale = new Vector3(200.0f, 10.0f, 200.0f);
target1.transform.parent = objectMiddle.transform;
}