private void FixedUpdate() { if (isMoving) { rb.velocity = speed * travelDirection; } Collider[] hitColliders = Physics.OverlapSphere(transform.position - (Vector3.up / 2), 0.05f); int i = 0; while (i < hitColliders.Length) { GroundPiece ground = hitColliders[i].transform.GetComponent <GroundPiece>(); if (ground && !ground.isColored) { ground.ChangeColor(solveColor); } i++; } if (nextCollisionPosition != Vector3.zero) { if (Vector3.Distance(transform.position, nextCollisionPosition) < 1) { isMoving = false; travelDirection = Vector3.zero; ///shouldnt be moving anymore nextCollisionPosition = Vector3.zero; } } if (isMoving) { return; } if (Input.GetMouseButton(0)) //either with finger or mouse on screen { swipePositionCurrentFrame = new Vector2(Input.mousePosition.x, Input.mousePosition.y); if (swipePositionLastFrame != Vector2.zero) { currentSwipe = swipePositionCurrentFrame - swipePositionLastFrame; if (currentSwipe.sqrMagnitude < minSwipeRecognition) { return; } currentSwipe.Normalize(); //getting the direction not the distance up left right down etc. if (currentSwipe.x > -0.5f && currentSwipe.x < 0.5) { //GO UP/DOWN SetDestination(currentSwipe.y > 0 ? Vector3.forward : Vector3.back); } if (currentSwipe.y > -0.5f && currentSwipe.y < 0.5) { //Go left/right SetDestination(currentSwipe.x > 0 ? Vector3.right: Vector3.left); } } swipePositionLastFrame = swipePositionCurrentFrame; } if (Input.GetMouseButtonUp(0)) { swipePositionLastFrame = Vector2.zero; currentSwipe = Vector2.zero; } }
private void FixedUpdate() { if (isTravelling) { rb.velocity = speed * travelDirection; } Collider[] hitColliders = Physics.OverlapSphere(transform.position - (Vector3.up / 2), 0.05f); int i = 0; while (i < hitColliders.Length) { GroundPiece ground = hitColliders[i].transform.GetComponent <GroundPiece>(); if (ground && !ground.isColored) { ground.ChangeColor(solveColor); } i++; } if (nectCollisionPosition != Vector3.zero) { if (Vector3.Distance(transform.position, nectCollisionPosition) < 1) { isTravelling = false; travelDirection = Vector3.zero; nectCollisionPosition = Vector3.zero; } } if (isTravelling) { return; } if (Input.GetMouseButton(0)) { swipePosCurrentFrame = new Vector2(Input.mousePosition.x, Input.mousePosition.y); if (swipePosLastFrame != Vector2.zero) { currentSwipe = swipePosCurrentFrame - swipePosLastFrame; if (currentSwipe.sqrMagnitude < minSwipeRecoginiton) { return; } currentSwipe.Normalize(); //Up/Down if (currentSwipe.x > -.5f && currentSwipe.x < .5f) { //go up/down SetDestionation(currentSwipe.y > 0 ? Vector3.forward : Vector3.back); } if (currentSwipe.y > -.5f && currentSwipe.y < .5f) { //go left/right SetDestionation(currentSwipe.x > 0 ? Vector3.right : Vector3.left); } } swipePosLastFrame = swipePosCurrentFrame; } if (Input.GetMouseButtonUp(0)) { swipePosLastFrame = Vector2.zero; currentSwipe = Vector2.zero; } }
private void FixedUpdate() { // Set the balls speed when it should travel if (isTraveling) { rb.velocity = travelDirection * speed; } // Paint the ground Collider[] hitColliders = Physics.OverlapSphere(transform.position - (Vector3.up / 2), .05f); int i = 0; while (i < hitColliders.Length) { GroundPiece ground = hitColliders[i].transform.GetComponent <GroundPiece>(); if (ground && !ground.isColored) { ground.ChangeColor(solveColor); } i++; } // Check if we have reached our destination if (nextCollisionPosition != Vector3.zero) { if (Vector3.Distance(transform.position, nextCollisionPosition) < 1) { isTraveling = false; travelDirection = Vector3.zero; nextCollisionPosition = Vector3.zero; } } if (isTraveling) { return; } // Swipe mechanism if (Input.GetMouseButton(0)) { // Where is the mouse now? swipePosCurrentFrame = new Vector2(Input.mousePosition.x, Input.mousePosition.y); if (swipePosLastFrame != Vector2.zero) { // Calculate the swipe direction currentSwipe = swipePosCurrentFrame - swipePosLastFrame; if (currentSwipe.sqrMagnitude < minSwipeRecognition) // Minium amount of swipe recognition { return; } currentSwipe.Normalize(); // Normalize it to only get the direction not the distance (would fake the balls speed) // Up/Down swipe if (currentSwipe.x > -0.5f && currentSwipe.x < 0.5f) { SetDestination(currentSwipe.y > 0 ? Vector3.forward : Vector3.back); } // Left/Right swipe if (currentSwipe.y > -0.5f && currentSwipe.y < 0.5f) { SetDestination(currentSwipe.x > 0 ? Vector3.right : Vector3.left); } } swipePosLastFrame = swipePosCurrentFrame; } if (Input.GetMouseButtonUp(0)) { swipePosLastFrame = Vector2.zero; currentSwipe = Vector2.zero; } }