// caution: this method could be run twice, not necessarily just once, right after the piece is instantiated void Start() { // initialize instance variables isNew = true; currPlacementCorrection = PlacementCorrectionType.None; collidersInContact = new List <Collider>(); canMoveDown = true; canMoveTowardsNegX = true; canMoveTowardsPosX = true; canMoveTowardsNegZ = true; canMoveTowardsPosZ = true; mainCamera = Camera.main; raycastingScript = GameObject.Find("Main Script Object").GetComponent <RaycastingBehaviour>(); GameObject floor = GameObject.Find("Floor"); floorY = floor.transform.position.y; pieces = raycastingScript.pieces; pieceControlsLabel = raycastingScript.pieceControlsLabel; pieceControlsPanel = raycastingScript.pieceControlsPanel; pieceSpecificSetup(); halo = getHalo(); savedTransforms = new Dictionary <GameObject, SavedTransformInfo>(); savedVelocities = new Dictionary <GameObject, SavedVelocityInfo>(); // move piece to position of touch (keeping it the same depth/distance from the camera as it was upon instantiation) // as seen in the fact that this is in Start, it should only happen upon instantiation. Position changes thereafter depend on how much the finger moves Vector3 currScreenPosition = mainCamera.WorldToScreenPoint(transform.position); Touch touch = Input.GetTouch(0); transform.position = mainCamera.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, currScreenPosition.z)); // leave the following line as the last line of the method setupComplete = true; }
void Update() { if (currPlacementCorrection == PlacementCorrectionType.ReturnToInitPosition) { if (initialPositionReturnFramesCompleted < initialPositionReturnTotalFrames && initialPositionReturnFrameTranslation.sqrMagnitude > FRAME_TRANSLATION_THRESHOLD) { transform.Translate(initialPositionReturnFrameTranslation, Space.World); initialPositionReturnFramesCompleted++; } else { if (isNew) { Destroy(gameObject); raycastingScript.ClearActivePiece(); } else { transform.position = initialPosition; currPlacementCorrection = PlacementCorrectionType.None; } DisconnectAndDestroy(positionTestObject); reactivateInteractables(); } } else if (currPlacementCorrection == PlacementCorrectionType.SnapTo) { if (placementCorrectionFramesCompleted < placementCorrectionTotalFrames && placementCorrectionFrameTranslation.sqrMagnitude > FRAME_TRANSLATION_THRESHOLD) { transform.Translate(placementCorrectionFrameTranslation, Space.World); placementCorrectionFramesCompleted++; } else { currPlacementCorrection = PlacementCorrectionType.None; transform.position = placementCorrectionTarget; initialPosition = placementCorrectionTarget; if (isNew) { isNew = false; pieces.Add(gameObject); } DisconnectAndDestroy(positionTestObject); reactivateInteractables(); } } else if (currPlacementCorrection == PlacementCorrectionType.SnapDown) { transform.Translate(placementCorrectionFrameTranslation); if (getBottom() < floorY || collidersInContact.Count > 0 || placementCorrectionFrameTranslation.sqrMagnitude < FRAME_TRANSLATION_THRESHOLD) // we've hit the piece below this one, or the floor; or the incremental movement is deemed too small for gradual placement correction { transform.Translate(-placementCorrectionFrameTranslation); // move back up a notch, so they're not actually overlapping currPlacementCorrection = PlacementCorrectionType.None; initialPosition = transform.position; if (isNew) { isNew = false; pieces.Add(gameObject); } DisconnectAndDestroy(positionTestObject); reactivateInteractables(); } } else if (moving && Input.touchCount > 0) { Touch touch = Input.GetTouch(0); if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled) { moving = false; PlacementCorrectionType availablePlacementCorrection = calculatePlacementCorrectionTarget(false); switch (availablePlacementCorrection) { case PlacementCorrectionType.SnapTo: // includes snapping to a designated target and snapping up out of the floor placementCorrectionTotalFrames = SNAP_SECONDS / Time.deltaTime; placementCorrectionFramesCompleted = 0; placementCorrectionFrameTranslation = (placementCorrectionTarget - transform.position) / placementCorrectionTotalFrames; currPlacementCorrection = PlacementCorrectionType.SnapTo; break; case PlacementCorrectionType.SnapDown: placementCorrectionFrameTranslation = (placementCorrectionTarget - transform.position) * Time.deltaTime / SNAP_SECONDS; currPlacementCorrection = PlacementCorrectionType.SnapDown; break; default: // piece was placed in an invalid location initialPositionReturnTotalFrames = SECONDS_TO_INITIAL_POSITION / Time.deltaTime; initialPositionReturnFramesCompleted = 0; initialPositionReturnFrameTranslation = (initialPosition - transform.position) / initialPositionReturnTotalFrames; currPlacementCorrection = PlacementCorrectionType.ReturnToInitPosition; break; } } else if (touch.phase == TouchPhase.Moved) { movePiece(touch.position); calculatePlacementCorrectionTarget(true); positionTestObject.transform.position = placementCorrectionTarget; } } }