Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Feedback How to place an object in a fixed position in the air?

Discussion in 'Handheld AR' started by eco_bach, Jun 5, 2019.

  1. eco_bach

    eco_bach

    Joined:
    Jul 8, 2013
    Posts:
    1,357
    So far have used target and planes to place my AR objects. I have seen demos where a user was able to place an object in a fixed position as though it were floating in the air.

    How is this accomplished and is there any demo I can reference?
     
  2. Tarrag

    Tarrag

    Joined:
    Nov 7, 2016
    Posts:
    120
    you could instantiate the object e.g. 3 meters in front of the camera.

    To make sure the object stays on that position, if you dont want to be raycasting directly to a ground plane, raycast down to a ground plane from a point in front of the camera and in the air that is 3 meters in front of the camera and instantiate then.

    Hope that makes sense.

    Cheers
     
    Last edited: Jun 5, 2019
  3. eco_bach

    eco_bach

    Joined:
    Jul 8, 2013
    Posts:
    1,357
    Thanks. Would you know if there is a demo in the ARKit Unity package that I could reference as a starting point?
     
  4. Tarrag

    Tarrag

    Joined:
    Nov 7, 2016
    Posts:
    120
    Sorry I don't. In ARFoundation this raycast overload can help you:

    public bool Raycast(Ray ray, List<ARRaycastHit> hitResults, TrackableType trackableTypeMask = TrackableType.All, float pointCloudRaycastAngleInDegrees = 5f);

    You can set the ray pointing down from an object e.g.3m in front of the camera.
     
    Last edited: Jun 7, 2019
  5. ArmanUnity

    ArmanUnity

    Joined:
    Nov 29, 2015
    Posts:
    21
    @Tarrag ... why we need a raycast to the ground plane???? doesnt it stay 3m in front of the camera always if we simply instantiate it in 3m forward ??
     
  6. Tarrag

    Tarrag

    Joined:
    Nov 7, 2016
    Posts:
    120
    I get more control of the obj's position if stuck to a plane
     
  7. ArmanUnity

    ArmanUnity

    Joined:
    Nov 29, 2015
    Posts:
    21
    so you suggesting cast a ray from 3m in front of the camera to the ground plane and if it is hitting on a trackable plane change the obj's position in accordance to the hit position
    ??
     
  8. Tarrag

    Tarrag

    Joined:
    Nov 7, 2016
    Posts:
    120
    Raycast 3m in front of cam then down to hit the plane yup. Along these lines:

    Ray ray = new Ray(obj.transform.position, Vector3.down);
    if(m_ArRaycastManagerScript.Raycast(ray, s_Hits, TrackableType.PlaneWithinPolygon))

    where obj is 3m in front of cam
     
  9. ArmanUnity

    ArmanUnity

    Joined:
    Nov 29, 2015
    Posts:
    21
    @Tarrag thanks for your helping replays .... what we need to do after the ray hit on the plane .... do we need to add some reference point in the hit position and make obj's as child or this reference point?????
    if then .can u give an example of adding reference point
     
  10. Tarrag

    Tarrag

    Joined:
    Nov 7, 2016
    Posts:
    120
    ArmanUnity likes this.
  11. ArmanUnity

    ArmanUnity

    Joined:
    Nov 29, 2015
    Posts:
    21
    okay.. thanks bro... am also figuring out to implement reference point:)