Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

ARKit How to translate an object along its anchor/detected plane

Discussion in 'AR' started by ItsFireMe, Feb 21, 2018.

  1. ItsFireMe


    Mar 6, 2013
    Hi all, Having a lot of fun with ARkit and Unity, But I'm not the best at code, more of a 3d artist than a coder/developer so I was just wondering if anyone can help me : -) with the issue that I have.

    What I'm trying to do is to get the placed object thats on the detected plane to have the ability to translate the object along that plane by touch for example " dragging the gameobject up on the device screen would move the object further away, Dragging down would move it closer and so on for left or right movement. but would stick to that plane in its Y position.

    I have got rotation working with the use of lean touch from the asset store which works great with the two finger gesture rotating the object around Y. but when I use lean touch translate I cant seem to get it to stay on the plane, and I have also had a look at a lot of different touch drag scripts online but they all seem to translate relative to the camera angle. So I was just wondering if anyone out there could point me in the right direction pardon the pun.

    Thanks in advance.
  2. ItsFireMe


    Mar 6, 2013
    Hi guys, I have sort of managed to come up with something that sort of works in a way, even though it isn't technically anchored to the detected plane. its still at the correct world Y position it was initially placed at so sort of correct in a way. but I am all ways up for suggestions to make it better, my is code bellow. Thanks

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    5. public class DragScriptTest : MonoBehaviour {
    7.     public GameObject gObj;
    8.     Plane objPlane;
    9.     Vector3 m0;
    11.     Ray GenerateMouseRay()
    12.     {
    13.         Vector3 mousePosFar = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, Camera.main.farClipPlane);
    14.         Vector3 mousePosNear = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane);
    15.         Vector3 mousePosF = Camera.main.ScreenToWorldPoint (mousePosFar);
    16.         Vector3 mousePosN = Camera.main.ScreenToWorldPoint (mousePosNear);
    18.         Ray mr = new Ray (mousePosN, mousePosF - mousePosN);
    19.         return mr;
    20.     }
    22.     // Update is called once per frame
    23.     void Update () {
    24.         if (Input.GetMouseButtonDown (0)) {
    25.             Ray mouseRay = GenerateMouseRay ();
    26.             RaycastHit hit;
    28.             if (Physics.Raycast (mouseRay.origin, mouseRay.direction, out hit)) {
    30.                 gObj = hit.transform.gameObject;
    31.                 objPlane = new Plane (Vector3.up, gObj.transform.position);
    33.                 //calc mouse offset
    34.                 Ray mRay = Camera.main.ScreenPointToRay (Input.mousePosition);
    35.                 float rayDistance;
    36.                 objPlane.Raycast (mRay, out rayDistance);
    37.                 m0 = gObj.transform.position - mRay.GetPoint (rayDistance);
    38.             }
    39.         } else if (Input.GetMouseButton (0) && gObj.gameObject.tag == "selectedObj") {
    40.             Ray mRay = Camera.main.ScreenPointToRay (Input.mousePosition);
    41.             float rayDistance;
    42.             if (objPlane.Raycast (mRay, out rayDistance))
    43.                 gObj.transform.position = mRay.GetPoint (rayDistance) + m0;
    44.         } else if (Input.GetMouseButtonUp (0) && gObj) {
    45.             gObj = null;
    46.         }
    48.     }
    49. }
  3. MadeFromPolygons


    Oct 5, 2013
    Basic algorithm:

    I use a custom plane with a bounds on it.

    Once placed on the plane, I get the direction etc that I want to go to. I do this by picking a random point within the bounds (just use the bounds.x and z, y can stay as whatever your object y pos should be for this plane (probably 0).

    Then i use simple vector math to determine the direction using that location and current transform location.

    Then after moving a bit in direction we check if we are still contained in the bounds. If not , pick a new random position from within the bounds and set that as direction and move towards it and repeat.

    You could use the planeAnchor.extents vector to determine the size of the plane but I havent found a way to get it to be accurate hence the custom plane object.
    Deleted User likes this.