1

Using ARCore on Unity, I'm trying to place one gameObject between two gameObjects. Unfortunately it doesn't display, it seems it is a problem with Anchor.

*Using Vuforia, i didn't had problem to position game objects.

What i try to achieve is:

  1. Place two objects (A and B) on the ground when i touch the surface. It Works
  2. Place a gameObject (C) between the two objects (A and B).Not displaying

    // 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);
    }
    }
    

EDIT

I successfully arrived to place the gameobject in between.

i added this last line in my code: 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;
    }
Rocé Tarentula
  • 653
  • 1
  • 6
  • 15
  • What does `it doesn't work` mean? Is it placed elsewhere / not placed at all? – derHugo Oct 30 '18 at 05:14
  • it does not work because you are using target1 and target2 positions. They are local positions relative to your anchors. You need to use the anchor positions for finding the middle position and also you must anchor that sphere as well. – Ali Kanat Oct 30 '18 at 09:54
  • 1
    Thanks for your helps, the gameobject didn't placed at all. I successfully arrived to place the object finally, watch my Edit in my post, i just added a line, Thanks again. – Rocé Tarentula Oct 30 '18 at 20:42

0 Answers0