I am new to AR development so sorry for this noob question. I am spawning an AR object in front of camera which spawns fine, but at some time, the object starts drifting in random directions. Can anyone help me with this issue? I have searched for quite some time now; I was unable to find the fix.
For Object Spawning
prefabInstance = Instantiate(placeObject);
GetComponent<ARSessionOrigin>().MakeContentAppearAt(prefabInstance.transform, new Vector3(0, -0.76f, 3.35f), Quaternion.identity);
I want shake to shatter the object in pieces. After the object is in pieces i relocate them in a different areas. I only relocate the parent object and pieces are in the child of the object.
Relocation Code
int temp = UnityEngine.Random.Range(0, ReferenceSpawnPoint.Length);
gObject.transform.position = new Vector3(ReferenceSpawnPoint[temp].position.x, ReferenceSpawnPoint[temp].position.y, ReferenceSpawnPoint[temp].position.z);
float y = UnityEngine.Random.Range(0, 360);
float z = UnityEngine.Random.Range(-10, 10);
gObject.transform.rotation = Quaternion.Euler(0, y, 0);
I am also attaching the link of the video; please take a look and you can understand what I am saying video link . For ease, please start from 0:30 sec mark.
Your help will be greatly appreciated.
Also sometimes I get a black screen; does this means that AR Session has not initialized?
Plugin/Tool Versions:
It seems like the way you are placing the object is wrong. A better way is to use raycasting and place the 3D object in some position defining by a user input such as touching the screen.
The AR Foundation sample scene has a script named PlaceOnPlane.cs that shows how you can detect when a user touches the screen and then raycast from it to the world to intatiate an game object:
if (Input.touchCount == 1) {
if (m_RaycastManager.Raycast(Input.GetTouch(0).position, s_Hits, TrackableType.PlaneWithinPolygon))
{
// Raycast hits are sorted by distance, so the first one
// will be the closest hit.
var hitPose = s_Hits[0].pose;
if (spawnedObject == null)
{
spawnedObject = Instantiate(m_PlacedPrefab, hitPose.position, hitPose.rotation);
}
}
Once you instantiate the model in a "real-world" position such as hitPose.position you shouldn't see it drift anymore.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.