//Check if pin is ready to be unlocked private IEnumerator CheckPin() { checkingPin = true; yield return(new WaitForSecondsRealtime(0.1f)); float currentState = SelectedAnim.GetCurrentAnimatorStateInfo(0).normalizedTime; float sweetSpot = SelectedPin.GetComponent <Behaviour_LockPin>().Fraction; if (Mathf.Abs(sweetSpot - currentState) < 0.01f) { sweetSpotReached = true; } else { sweetSpotReached = false; } checkingPin = false; }
//Lock pin state and exclude from selectable pins private void HoldPin() { Behaviour_LockPin controls = SelectedPin.GetComponent <Behaviour_LockPin>(); controls.dropped = true; controls.holding = true; pinsLeft = new List <GameObject>(); pinsLeft.AddRange(UI_Lock.ActivePins.Except(HoldPins)); HoldPins.Add(SelectedPin); var tempSelectedPin = SelectedPin; if (SelectedPin != pinsLeft[pinsLeft.Count - 1]) { SelectedPin = pinsLeft[pinsLeft.FindIndex(x => x == SelectedPin) + 1]; } else if (SelectedPin != pinsLeft[0]) { SelectedPin = pinsLeft[pinsLeft.FindIndex(x => x == SelectedPin) - 1]; } pinsLeft.Remove(tempSelectedPin); CollectPrecedingPins(); currentPosition += 1; if (currentPosition >= UI_Lock.ActivePins.Count) { if (!UI_Lock.currentLock.unlocked) { UI_Lock.currentLock.unlocked = true; } else { UI_Lock.currentLock.unlocked = false; } SelectedAnim.Rebind(); gameObject.SetActive(false); } }
/* * On Update do: * >>Attached? * - Check if tomato should be discarted (average speed above given threshold * - Check if tomato should be put in the box * - Move (follow hand) * >>Not Attached? * - Check if it should be attached to players hand * - Move (to position / around the position) */ void Update () { /* * Movement Handler */ currentPosition = transform.position; // Set the current position of the Tomato switch (state) { case TomatoState.notActive: // Try to reach target applyGravity(); if (speed.y<0) // If tomato is falling flapWings(); applySpeed(); // If the tomato goes close to the target set as Active if (Vector3.Distance(currentPosition, targetPosition.position)<1f){ musicSource.clip = selected; musicSource.Play(); state = TomatoState.Active; SelectedAnim.Play(); // Play the animation for selection tomatoSpeed *= 0.3f; } break; case TomatoState.Active: // Try to keep still near target applyGravity(); // If tomato is really falling & is below the target if (speed.y<0.5f && currentPosition.y < targetPosition.position.y) flapWings(); applySpeed(); break; case TomatoState.Attached: // Stay attached moveToParentHolder(); // Move to the parentHolder position break; case TomatoState.Dropped: // Drop applyGravity(); // Apply gravitational force to speed applySpeed(); // Apply the speed to position checkForDeletion(); // Check whether the object is off screen and delete if so break; case TomatoState.Inbox: // Destroy Self Destroy(this.gameObject); // Kill the gameObject (Tomato) break; } // If the tomato is attached to either hand Check whether the average speed indicates it should be thrown away if (state.Equals(TomatoState.Attached)) { // Calculate the average speed over the last 10 frames (units/sec) // Frame-to-frame speed values are kept in an Array (speedValues) which is limited to 10 elements float averageSpeed = 0; currentPosition = transform.position; speedValues.Insert(0,(float) Mathf.Abs(currentPosition.x - oldPosition.x)/Time.deltaTime); // Insert the new speed first if (speedValues.Count>10) speedValues.RemoveAt(10); // Remove the last element foreach(float speed in speedValues){ // Calculate the average speed averageSpeed += speed/10; } // If the average speed in the duration of the last 10 frames exceeds // the input threshold, discard the tomato object if (averageSpeed >= speedThreshold) { musicSource.clip = canceled; musicSource.Play(); setSpeed(); // Set the speed variable to the actual frame-to-frame speed state = TomatoState.Dropped; } } oldPosition = currentPosition; // The old position will be the current position Ray myFrontRay = new Ray(transform.position,Vector3.back); Ray myBacktRay = new Ray(transform.position,-1*Vector3.back); if (Physics.Raycast(myFrontRay,out hittingFront,100f) || Physics.Raycast(myBacktRay,out hittingBack,100f)) { // Get the hitting object that got the trigger (prefer front) if (hittingFront.transform != null) hitting = hittingFront; else hitting = hittingBack; if (state.Equals(TomatoState.Active) && hitting.transform.tag == "SelectingBodyPart" && hitting.transform.GetComponentInChildren<ItemInHand>()!=null && hitting.transform.GetComponentInChildren<ItemInHand>().containing == null ) { musicSource.clip = selected; musicSource.Play(); state = TomatoState.Attached; gameObject.SampleAnimation(SelectedAnim.clip, 0); // Set the selected animation to frame 0 (nothing visible) parentHolder = hitting.transform; // Find the ItemInHand script on the hand and set the containing object parentHolder.GetComponentInChildren<ItemInHand>().containing = this.transform; tomatoMaker.setInstantiated(number, false); // Create a new tomato } // The tomato is already attached to the hand , now just check that you are hitting the basket if (state.Equals(TomatoState.Attached) && hitting.transform.name == "Select") { // Put the value in the tomato in the basket hitting.transform.GetComponent<BoxContaining>().AddValue(number); parentHolder = hitting.transform; // Set the box as the parent holder moveToParentHolder(); // Move to the parent holder position state = TomatoState.Inbox; } } }
// Update is called once per frame void Update() { lockAxis = CrossPlatformInputManager.GetAxis("GunVertical") % 1; if (CrossPlatformInputManager.GetButtonDown("Horizontal") && SelectedAnim.GetCurrentAnimatorStateInfo(0).normalizedTime >= 1) { ChangeSelectedPin(CrossPlatformInputManager.GetAxis("Horizontal")); } if (CrossPlatformInputManager.GetButton("Fire2") && lockAxis >= 0 && SelectedAnim.GetCurrentAnimatorStateInfo(0).normalizedTime > 0) { SelectedAnim.SetFloat("Speed", -lockAxis); foreach (var anim in PrecedingAnims) { if ((anim.GetCurrentAnimatorStateInfo(0).normalizedTime - SelectedAnim.GetCurrentAnimatorStateInfo(0).normalizedTime) > 0.42f) { anim.SetFloat("Speed", -lockAxis); } else if (anim.GetCurrentAnimatorStateInfo(0).normalizedTime <= 1) { anim.SetFloat("Speed", 1); } else { anim.SetFloat("Speed", 0); } } } else if (SelectedAnim.GetCurrentAnimatorStateInfo(0).normalizedTime <= 1) { SelectedAnim.SetFloat("Speed", 1); foreach (var anim in PrecedingAnims) { if (anim.GetCurrentAnimatorStateInfo(0).normalizedTime <= 1) { anim.SetFloat("Speed", 1); } else { anim.SetFloat("Speed", 0); } } } else { SelectedAnim.SetFloat("Speed", 0); PrecedingAnims.ForEach(x => x.SetFloat("Speed", 0)); } if (!checkingPin && SelectedPin == UI_Lock.PinOrder[currentPosition]) { StartCoroutine(CheckPin()); } if (SelectedAnim.GetCurrentAnimatorStateInfo(0).normalizedTime < 0.56f) //Change dropped lower pin position if pick has reached them { HoldPinResidue.ForEach(x => x.droppedPosition = new Vector3(x.droppedPosition.x, pick.GetChild(0).position.y)); } if (sweetSpotReached) { UI_Lever.SetLeverStatus(true); if (CrossPlatformInputManager.GetButtonDown("Fire1")) { HoldPin(); } } else { UI_Lever.SetLeverStatus(false); } if (CrossPlatformInputManager.GetButton("Cancel")) // If Cancel Pressed disable lock UI { gameObject.SetActive(false); } }