Hello here is what I did, I want to move my AR object that is in a MidAirStage to move to the top left corner of the screen.
If I mode the object in the editor to the cordinates printed the position is correct however when I call the method at runtime the object moves to a completly differnt position. What am I doing wrong? Open to other solutions as well. Thank you.
private void UpdateModelPosition()
{
Bounds _bounds = loadedModel.GetComponentInChildren<MeshFilter>().mesh.bounds;
// Move model to the center of pivot.
loadedModel.AddComponent<Rigidbody>().useGravity = false;
loadedModel.transform.SetParent(modelPivot.transform);
loadedModel.transform.position -= _bounds.center - loadedModel.transform.position;
// Move model to the correct corner of pivot.
float _widthSize = _bounds.size.x;
float _newX = 0;
float _heightSize = _bounds.size.y;
float _newY = 0;
switch (ModelPosition)
{
case ModelPositionType.TopLeft:
_newX += -_widthSize / 2;
_newY += -_heightSize / 2;
break;
case ModelPositionType.TopMiddle:
_newY += -_heightSize / 2;
break;
case ModelPositionType.TopRight:
_newX += _widthSize / 2;
_newY += -_heightSize / 2;
break;
case ModelPositionType.MiddleLeft:
_newX += -_widthSize / 2;
break;
case ModelPositionType.MiddleRight:
_newX += _widthSize / 2;
break;
case ModelPositionType.BottomLeft:
_newX += -_widthSize / 2;
_newY += _heightSize / 2;
break;
case ModelPositionType.BottomMiddle:
_newY += _heightSize / 2;
break;
case ModelPositionType.BottomRight:
_newX += _widthSize / 2;
_newY += _heightSize / 2;
break;
case ModelPositionType.Custom:
_newX += _widthSize / 2;
_newY += -_heightSize / 2;
break;
}
loadedModel.transform.position = new Vector3(loadedModel.transform.position.x + _newX, loadedModel.transform.position.y + _newY, 0);
modelPivot.transform.SetPositionAndRotation(Vector3.zero, Quaternion.identity);
//Move pivot to the correct screen corner.
Camera _camera = videoRecorderController.NotRecordingCamera;
float _screenHeight = Screen.height;
float _screenWidth = Screen.width;
float _screenDepth = loadedModel.transform.position.z - _camera.transform.position.z;
Vector3 _screenToWorldPoint = _camera.ScreenToWorldPoint(new Vector3(0, _screenHeight, _screenDepth)); // Top left corner.
loadedModel.transform.position = new Vector3(_screenToWorldPoint.x, _screenToWorldPoint.y, loadedModel.transform.position.z);
Debug.Log(_screenToWorldPoint);
}