I am using LeanTouch+ to translate object but it can only move on Y based on the android screen, so on the camera the object can only be moved up and down. What i am trying to achieve is to be able to move the object further or closer to the camera view. In ARCore you first detect the environment and then it is generated as Plane and thats the plane i need the object to move around to.
Also if possible i want to use Clamp on the "detected plane" to restrict movement further than the detected plane
here is my code for spawning the obj using LeanFingerTap
public void Spawn(LeanFinger finger)
{
if (AndyPlanePrefab != null && finger != null)
{
// Raycast against the location the player touched to search for planes.
TrackableHit hit;
TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon |
TrackableHitFlags.FeaturePointWithSurfaceNormal;
if (Frame.Raycast(finger.ScreenPosition.x, finger.ScreenPosition.y, raycastFilter, out hit))
{
if(currentNumberofPrefab<numberOfPrefabsAllowed)
{
currentNumberofPrefab = currentNumberofPrefab + 1;
// Use hit pose and camera pose to check if hittest is from the
// back of the plane, if it is, no need to create the anchor.
if ((hit.Trackable is DetectedPlane) &&
Vector3.Dot(FirstPersonCamera.transform.position - hit.Pose.position,
hit.Pose.rotation * Vector3.up) < 0)
{
Debug.Log("Hit at back of the current DetectedPlane");
}
else
{
// Choose the Andy model for the Trackable that got hit.
if (hit.Trackable is FeaturePoint)
{
AndyPrefab = AndyPointPrefab;
}
else
{
getValue = ItemScrollList.valuePrefab;
if (getValue == 1)
{
AndyPrefab = AndyPlanePrefab[0];
Debug.Log("value 1");
}
else if (getValue == 2)
{
AndyPrefab = AndyPlanePrefab[1];
Debug.Log("value 2");
}
else if (getValue == 3)
{
AndyPrefab = AndyPlanePrefab[2];
Debug.Log("value 3");
}
else if (getValue == 4)
{
AndyPrefab = AndyPlanePrefab[3];
Debug.Log("value 4");
}
else if (getValue == 5)
{
AndyPrefab = AndyPlanePrefab[4];
Debug.Log("value 5");
}
}
// Instantiate Andy model at the hit pose.
var andyObject = Instantiate(AndyPrefab, hit.Pose.position, hit.Pose.rotation);
//adding tag to andyObject for destroying spawn purposes
andyObject.tag = "HomePrefab";
// Compensate for the hitPose rotation facing away from the raycast (i.e. camera).
andyObject.transform.Rotate(0, k_ModelRotation, 0, Space.Self);
// Create an anchor to allow ARCore to track the hitpoint as understanding of the physical
// world evolves.
var anchor = hit.Trackable.CreateAnchor(hit.Pose);
// Make Andy model a child of the anchor.
andyObject.transform.parent = anchor.transform;
}
}
}
}
}
You can achieve this by creating a prefab which has an empty game object as parent and your object as child in (0, 0, 0)
.
Then when you detect the plane you get the plane using Session.GetTrackable
in to a list.
Then you can instantiate your prefab in the center of detected plane using this piece of code:
Anchor anchor = m_AllPlanes[0].CreateAnchor(m_AllPlanes[0].CenterPose);
var obj = Instantiate(myPrefab, m_AllPlanes[0].CenterPose);
obj.transform.parent = anchor.transform;
Then you can move obj in local z if you want it to go further or closer to camera. Your clamp then can be m_AllPlanes[0].CenterPose.position.z - m_AllPlanes[0].ExtentZ
to m_AllPlanes[0].CenterPose.position.z + m_AllPlanes[0].ExtentZ
The reason why i suggested creating a prefab with a game object is to be able to work with local coordinate since ARCore coordinates changes in every Session.