public void OnLevelComplete() { // Force the current interactable to be let go if (currentInteractable != null) { currentInteractable.active = false; currentInteractable = null; } m_Animator.SetTrigger("Fling"); m_LevelHolder.OnLevelComplete(); m_MusicController.phase++; }
// Update is called once per frame void Update() { // Moving left-right float leftRight = Input.GetAxis("Horizontal"); if (Mathf.Abs(leftRight) > 0.1f) { // Target facing value should be close to either -1 or 1 // Cube root the value to keep its sign and bring it closer to a magnitude of 1 targetFacingDirection = Mathf.Sign(leftRight) * Mathf.Pow(Mathf.Abs(leftRight), 1.0f / 3.0f); // Apply the actual genuine value to the velocity of the rigidbody m_Rigidbody2D.velocity = Vector3.SmoothDamp(m_Rigidbody2D.velocity, new Vector2(leftRight * m_Speed, m_Rigidbody2D.velocity.y), ref speedVelocity, m_Smoothing); } actualFacingDirection = Mathf.SmoothDamp(actualFacingDirection, targetFacingDirection, ref facingDirectionVelocity, m_Smoothing); m_Animator.SetFloat("FacingDirection", actualFacingDirection); // Try to align the robo such that it is facing the ground below it RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.down, 2.0f, Physics.DefaultRaycastLayers & ~LayerMask.GetMask("Player")); float floorAngle = 0.0f; if (hit.collider != null) { floorAngle = Mathf.Atan2(-hit.normal.x, hit.normal.y) * Mathf.Rad2Deg; } correctionAngle = Vector3.SmoothDamp(correctionAngle, new Vector3(0, 0, floorAngle), ref rotationVelocity, 0.2f); transform.localEulerAngles = correctionAngle; // Try to anchor the main object to the current level as best as possible transform.position = new Vector3(transform.position.x, transform.position.y, m_LevelHolder.GetLevelDepth() + (m_FollowLevelDepth ? 0.0f : -15.0f)); if (Input.GetMouseButtonDown(0)) { // See if we've hit an interactable or not BeamInteractable[] targets = m_LevelHolder.GetLevelInteractables(); foreach (BeamInteractable target in targets) { // Cast the mouse position to this interactable Vector3 relativeToCamera = m_Camera.transform.worldToLocalMatrix.MultiplyPoint(target.transform.position); Vector3 clickWorldPosition = m_Camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, relativeToCamera.z)); if ((target.transform.position - clickWorldPosition).magnitude < 5.0f) { // Close enough to hit if (currentInteractable == null || (currentInteractable != null && (target.transform.position - clickWorldPosition).magnitude < (currentInteractable.transform.position - clickWorldPosition).magnitude)) { if (currentInteractable != null) { currentInteractable.active = false; } currentInteractable = target; target.active = true; } } } } if (!Input.GetMouseButton(0) && currentInteractable != null) { currentInteractable.active = false; currentInteractable = null; } lightBrightness = Mathf.SmoothDamp(lightBrightness, currentInteractable != null ? 1.0f : 0.0f, ref lightBrightnessVelocity, 0.25f);; m_Animator.SetFloat("LightBrightness", lightBrightness); // Do beam if (currentInteractable == null) { m_LineRenderer.enabled = false; } else { m_LineRenderer.enabled = true; Vector3 interactableStop = currentInteractable.transform.position + (2 * Vector3.back); Vector3 interactableEntry = currentInteractable.transform.position + (10 * Vector3.back); Vector3[] positions = new Vector3[16]; for (int i = 0; i < 16; i++) { positions[i] = BezierTerm((float)i / 15, m_BeamOrigin.position, m_BeamHandle.position, interactableEntry, interactableStop); } m_LineRenderer.SetPositions(positions); // Cast the mouse position to this interactable Vector3 relativeToCamera = m_Camera.transform.worldToLocalMatrix.MultiplyPoint(currentInteractable.transform.position); currentInteractable.target = m_Camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, relativeToCamera.z)); } // Interaction music m_MusicController.interactionOn = (currentInteractable != null); // Reset if (Input.GetKeyDown(KeyCode.R)) { transform.position = m_LevelHolder.GetResetPosition(); m_RespawnSound.Play(); } // Debug: skip level if (Application.isEditor && Input.GetKeyDown(KeyCode.N)) { transform.position = m_LevelHolder.GetEndPosition(); } }