Rotate and Size a GameObject in ARCore
Hello everybody I've this problem, using ARCore example scene I'm trying to rotate and Scale the AndyGameObj using my touch. Using the PlayerPrefInt "Status" 0 or 1 trying to divide the "game" in 2 parts, first you put the gameobj in the scene and than you interact with him. I can't figure put why I can't interact whit Andy o.o
this is my script:
public void Update() {
if (PlayerPrefs.GetInt("Status") == 0)
{
if (Input.GetKey(KeyCode.Escape))
{
Application.Quit();
}
_QuitOnConnectionErrors();
// Check that motion tracking is tracking.
if (Frame.TrackingState != TrackingState.Tracking)
{
const int lostTrackingSleepTimeout = 15;
Screen.sleepTimeout = lostTrackingSleepTimeout;
return;
}
Screen.sleepTimeout = SleepTimeout.NeverSleep;
Frame.GetPlanes(m_NewPlanes, TrackableQueryFilter.New);
for (int i = 0; i < m_NewPlanes.Count; i++)
{
GameObject planeObject = Instantiate(TrackedPlanePrefab, Vector3.zero, Quaternion.identity,
transform);
planeObject.GetComponent<TrackedPlaneVisualizer>().Initialize(m_NewPlanes[i]);
}
// Disable the snackbar UI when no planes are valid.
Frame.GetPlanes(m_AllPlanes);
bool showSearchingUI = true;
for (int i = 0; i < m_AllPlanes.Count; i++)
{
if (m_AllPlanes[i].TrackingState == TrackingState.Tracking)
{
showSearchingUI = false;
break;
}
}
SearchingForPlaneUI.SetActive(showSearchingUI);
// If the player has not touched the screen, we are done with this update.
Touch touch;
if (Input.touchCount < 1 || (touch = Input.GetTouch(0)).phase != TouchPhase.Began)
{
return;
}
// Raycast against the location the player touched to search for planes.
TrackableHit hit;
TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinBounds | TrackableHitFlags.PlaneWithinPolygon;
if (Session.Raycast(touch.position.x, touch.position.y, raycastFilter, out hit))
{
var andyObject = Instantiate(AndyAndroidPrefab, hit.Pose.position, hit.Pose.rotation);
// Create an anchor to allow ARCore to track the hitpoint as understanding of the physical
// world evolves.
var anchor = hit.Trackable.CreateAnchor(hit.Pose);
// Andy should look at the camera but still be flush with the plane.
andyObject.transform.LookAt(FirstPersonCamera.transform);
andyObject.transform.rotation = Quaternion.Euler(0.0f, andyObject.transform.rotation.eulerAngles.y, andyObject.transform.rotation.z);
// Make Andy model a child of the anchor.
andyObject.transform.parent = anchor.transform;
Debug.Log("PIAZZATO");
}
PlayerPrefs.SetInt("Status", 1);
}
else if (PlayerPrefs.GetInt("Status") == 1)
{
GameObject objectInGame = GameObject.Find("Model");
for (int i = 0; i < Input.touchCount; i++)
{
if (i == 0)
{
Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
objectInGame.transform.Rotate(Vector3.up -touchDeltaPosition.x Time.deltaTime * rotationSpeed); } else if (i == 1) {
Touch touch0 = Input.GetTouch(0);
Touch touch1 = Input.GetTouch(1);
if (touch0.phase == TouchPhase.Moved && touch1.phase == TouchPhase.Moved)
{
Vector2 prevDist = (touch0.position - touch0.deltaPosition) - (touch1.position - touch1.deltaPosition);
Vector2 curDist = touch0.position - touch1.position;
float delta = curDist.magnitude - prevDist.magnitude;
defaultScaleX = objectInGame.transform.localScale.x;
defaultScaleY = objectInGame.transform.localScale.y;
defaultScaleZ = objectInGame.transform.localScale.z;
if (delta > 0)
{
//if (defaultScaleX >= currentScaleX * 4)
//{
//}
//else if (defaultScaleX < currentScaleX - 0.1)
//{
//}
//else
// if (defaultScaleX < currentScaleX)
defaultScaleX = defaultScaleX + 2.5f;
defaultScaleY = defaultScaleY + 2.5f;
defaultScaleZ = defaultScaleZ + 2.5f;
objectInGame.transform.localScale = new Vector3(defaultScaleX, defaultScaleY, defaultScaleZ);
}
else if (delta < 0)
{
defaultScaleX = defaultScaleX - 2.5f;
defaultScaleY = defaultScaleY - 2.5f;
defaultScaleZ = defaultScaleZ - 2.5f;
objectInGame.transform.localScale = new Vector3(defaultScaleX, defaultScaleY, defaultScaleZ);
}
}
}
}
}
}
Comment