void LateUpdate() { t -= Time.deltaTime; if (t < 0) { t = 0.1f; Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f)); // Ray ray = Camera.main.ScreenPointToRay(Camera.main.transform.forward); float radius = 0.05f; bool hitt = false; foreach (RaycastHit hit in Physics.SphereCastAll(ray, radius)) { // Debug.Log("hit;"+hit.collider.name); FlyingOnion fo = hit.collider.gameObject.GetComponent <FlyingOnion> (); DishOnion disho = hit.collider.gameObject.GetComponent <DishOnion>(); if (disho) { // Debug.Log("Showy"); if (ItemPopup.instance) { ItemPopup.instance.Show(disho.transform.position + Vector3.up * 0.25f, "Destroy this! " + disho.DishesRemainingInfo[0].ToString() + " of " + disho.DishesRemainingInfo[1].ToString() + " dishes remaining."); } break; } if (fo) { fo.CameraHovering(); // Let onion "know" it has been seen -- causes onion to move for 1-2 seconds } DamageReceiver dr = hit.collider.gameObject.GetComponent <DamageReceiver> (); if (dr || fo || disho) { hitt = true; Crosshair.instance.SetState(Crosshair.State.Destructible); } } if (!hitt) { Crosshair.instance.SetState(Crosshair.State.Nominal); } } }
public bool FoundTargetNearOnion(FlyingOnion mo) { if (targetFeatureClusterTimer < 0) { foundAnyQuadrant = false; if (lastOnionWhoRequested != mo) { searchRadius = 8f; } lastOnionWhoRequested = mo; targetFeatureClusterTimer = targetFeatureClusterInterval; // InitQuadrantScores (); QuadrantScores.Clear(); // Let's make 27 quadrants relative to onion's world position // Check each quadrant for total green features // The quadrant with the most green features is where the onion moves. // iterate through all Green map points #if !UNITY_EDITOR Vector3[] greenPoints = Utils2.PlaceNoteGreenPoints(0); #else Vector3[] greenPoints = GenerateFakeGreenPointsForEditor(); #endif Debug.Log("Greenpoints:" + greenPoints); foreach (Vector3 gp in greenPoints) { for (int i = -1; i < 2; i++) { for (int j = -1; j < 2; j++) { for (int k = -1; k < 2; k++) { // For each point compare its distance to one of 27 quadrants surrounding the onion // Closer quadrants are scored more highly // Result should be that the quadrant with the most green dots gets the highest score. float distFromThisQuadrantToGreenPoint = Vector3.SqrMagnitude(mo.transform.position + new Vector3(i, j, k) * searchRadius - gp); Vector3 key = new Vector3(i, j, k); float val = Mathf.Min(1, 1 / distFromThisQuadrantToGreenPoint); // Debug.Log ("val for " + i + "," + j + "," + k + ": " + val); // if (QuadrantScores.ContainsKey (key)) { // QuadrantScores [key] += val; // never more than 1 // } else { // QuadrantScores.Add (key, val); // } } } } } // float max = 0; Vector3 bestQuadrant = GetBestQuadrant(); if (foundAnyQuadrant) { Debug.Log("set cached target;" + mo.transform.position + " plus <color=green>" + bestQuadrant + "</color>"); cachedTarget = mo.transform.position + bestQuadrant; // If the best quadrant is zero, shrink the radius if (bestQuadrant == Vector3.zero) { searchRadius /= 2f; Debug.Log("<color=blue>Decereased by half. New radius:" + searchRadius + "</color>"); } } else { Debug.Log("Found no quads"); } Debug.Log("<color=blue>Cached :" + cachedTarget + "</color>"); } return(foundAnyQuadrant); }