// Use this for initialization void Start() { //find and get references to end cube and gamemanager endCube = GameObject.Find("End Cube"); gameManagerScr = GameObject.Find("_GameManager").GetComponent <S_GameManager>(); gameManagerScr.UpdateScores(); //update UI on game start playerMat = gameObject.GetComponent <Renderer>().material; //get the material of the player object (mirror or player) }
//Function called when a single shield is purchased with coins public void OnShopShieldClick() { if (gameManagerScr.coins >= 5) //if the player has enough coins for item { //buy shield gameManagerScr.coins -= 5; //reduce coin count by cost of item gameManagerScr.shieldCount++; //increase shield count gameManagerScr.UpdateScores(); //update UI stats //play positive feedback sound of purchase mySound.PlayOneShot(gameManagerScr.buySound, gameManagerScr.SFXVolume); } else //if player doesn't have enough coins for item { Debug.Log("Not Enough Coins!"); //play negative feedback sound mySound.PlayOneShot(gameManagerScr.failedLevelSound, gameManagerScr.SFXVolume); } }
//Function called when shop button is clicked on main menu public void ShopClicked() { TriggerAnimations(); //trigger the parting animation of UI shopImage.SetActive(true); //display shop UI gameManagerScr.UpdateScores(); //update UI //play feedback sound if (!mySound.isPlaying) { mySound.PlayOneShot(gameManagerScr.buttonClickSound, gameManagerScr.SFXVolume); } }
//Function called when the player or mirror object enters a collision with another object private void OnTriggerEnter(Collider other) { //if the object is the end cube and its in the tutorial, move to the next tutorial stage if (other.gameObject == endCube && gameObject.name == "Player" && gameManagerScr.tutorialCount < 3) { gameManagerScr.NextTutorialStage(); } else if (other.gameObject == endCube && gameObject.name == "Player" && gameManagerScr.tutorialCount == 3) { //if the object is the end cube and the tutorial is finished then call next level gameManagerScr.bTutorialFinished = true; //bool to let other scripts know tutorial is finished gameManagerScr.OnLevelCompleted(); } //if the object is a barrier if (other.gameObject.tag == "Barrier" && gameObject.tag == "Player") { bInCollision = true; //lock used to only run code once per object if (gameManagerScr.shieldCount > 0 && !gameManagerScr.bInvincible) //if player has a shield active and isnt in invincible state { //play negative feedback sound gameManagerScr.FailedLevelSound(); gameManagerScr.bInvincible = true; //set player and mirror to invincible state Invoke("ResetInvincibility", 2.0f); //start a timer to reset the invincibility after 2 seconds gameManagerScr.shieldCount--; //reduce players shield count by one gameManagerScr.UpdateScores(); //update UI gameObject.GetComponent <Renderer>().material = shieldMat; //set objects material to invincible texture //apply material to opposite object also if (gameObject.name == "Player") { otherMat = GameObject.Find("Mirror").GetComponent <Renderer>().material; GameObject.Find("Mirror").GetComponent <Renderer>().material = shieldMat; } else { otherMat = GameObject.Find("Player").GetComponent <Renderer>().material; GameObject.Find("Player").GetComponent <Renderer>().material = shieldMat; } } else if (gameManagerScr.shieldCount <= 0 && !gameManagerScr.bInvincible) //if player doesn't have a shield available { gameManagerScr.OnLevelFailed(); //call level failed function to reset the level } } if (other.gameObject.tag == "Coin") //if the collision is with a coin object { gameManagerScr.CoinCollectSound(); //play coin collect sound other.GetComponent <Collider>().enabled = false; //turn the coins collider off to stop it triggering if it hits the other object on way to top UI StartCoroutine(DeleteCoin(other.gameObject)); //use co-routine to move, expand and then delete coin object } }
void Update() { if (gameManagerScr.bGameInProgress) //only performs checks if the game is in progress { if (!Application.isMobilePlatform) //platform specific input { cameraRay = mainCamera.ScreenPointToRay(Input.mousePosition); //create a raycast to the mouse position Plane groundPlane = new Plane(Vector3.up, Vector3.zero); float rayLength; if (groundPlane.Raycast(cameraRay, out rayLength)) //if the mouse is over the plane { Vector3 pointToMove = (cameraRay.GetPoint(rayLength) - transform.position); // set a point to move towards moveInput = new Vector3(pointToMove.x, transform.position.y, pointToMove.z); //create a vector with the correct y value to move towards if (moveInput.sqrMagnitude <= 0.6f) //dead zone to stop jittery movement { moveInput = new Vector3(0.0f, transform.position.y, 0.0f); //set movement to 0 } moveInput = Vector3.Normalize(moveInput); //normalise the move vector to ensure its always the same speed } moveVelocity = moveInput * playerSpeed * 70; //multiply the vector by a constant speed value (*70 just makes the playerSpeed a nicer number) //collision checks to limit movement of other object, mirror/player, based on collisions if (!playerCollScr.bInCollision) //if player isn't in a collision then the mirror can move { mirrorRigidbody.velocity = new Vector3(-1.0f * moveVelocity.x, moveVelocity.y, -1.0f * moveVelocity.z); } if (!mirrorCollScr.bInCollision) //if mirror isn't in a collision then the player can move { playerRigidbody.velocity = moveVelocity; } if (mirrorCollScr.bInCollision && playerCollScr.bInCollision) //if both objects are in a collision, both can move to avoid getting stuck { playerRigidbody.velocity = moveVelocity; mirrorRigidbody.velocity = new Vector3(-1.0f * moveVelocity.x, moveVelocity.y, -1.0f * moveVelocity.z); } //check to fix displacement errors if mirror object moves out of sync with the player position if (!mirrorCollScr.bInCollision && !playerCollScr.bInCollision && mirror.transform.position.x != (player.transform.position.x * -1) && mirror.transform.position.z != player.transform.position.z * -1) { Vector3 oppositePos = player.transform.position * -1; oppositePos.y = player.transform.position.y; mirror.transform.position = oppositePos; } //used to activate timewarp if (gameManagerScr.timeWarpCount > 0) //if the player has bought a timewarp { //if the player clicks the screen and not a UI element if (bReadyToClick && Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject()) { gameManagerScr.timeWarpCount--; //reduce number of timewarps available gameManagerScr.UpdateScores(); //update UI bReadyToClick = false; //lock to stop them using two timewarps by accident timeImage.SetActive(true); //give visual feedback that the timewarp is active Time.timeScale = 0.25f; //slow down time to a quarter speed Invoke("ResetSpeed", 3f); //in 3 seconds (12 seconds realtime) reset the speed Invoke("StartFlash", 1f); //start the flashing effect after 1 third of the time } } } else if (Application.isMobilePlatform) //mobile specific movement input { /////////////// Start of adapted code from N3K EN, 2017 ////////////////////// Vector3 tilt = Input.acceleration - startTilt; //work out the tilt displacement from the start position if (isFlat) { tilt = Quaternion.Euler(90, 0, 0) * tilt * playerSpeed * 100; //set the vector to be the correct rotation (device face up) } //code to limit movement on mobile based on collisions if (!playerCollScr.bInCollision) //if player isn't in collision, allow movement { mirrorRigidbody.velocity = new Vector3(-1.0f * tilt.x, tilt.y, -1.0f * tilt.z); } if (!mirrorCollScr.bInCollision) //if mirror isn't in collision, allow movement { playerRigidbody.velocity = tilt; } if (mirrorCollScr.bInCollision && playerCollScr.bInCollision) //if both are in collision, allow movement to stop getting stuck { playerRigidbody.velocity = tilt; mirrorRigidbody.velocity = new Vector3(-1.0f * tilt.x, tilt.y, -1.0f * tilt.z); } /////////////// End of adapted code from N3K EN, 2017 ////////////////////// //used to activate timewarp on mobile platform if (gameManagerScr.timeWarpCount > 0) //if player has bought a timewarp { if (bReadyToClick && Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began) //if they tap the screen { if (!EventSystem.current.IsPointerOverGameObject(Input.touches[0].fingerId)) //check tap isn't on UI element { //perform same actions as pc platform gameManagerScr.timeWarpCount--; gameManagerScr.UpdateScores(); bReadyToClick = false; timeImage.SetActive(true); Image timeImageImg = timeImage.GetComponent <Image>(); timeImageImg.color = new Color(timeImageImg.color.r, timeImageImg.color.g, timeImageImg.color.b, 255); Time.timeScale = 0.25f; //slow time down in the game //start timers for resetting speed and displaying flash Invoke("ResetSpeed", 3f); Invoke("StartFlash", 1f); } } } } } else { //fail safe to stop any movement whilst the game is not in progress playerRigidbody.velocity = Vector3.zero; mirrorRigidbody.velocity = Vector3.zero; } }