public Vector2?intersectswithMe(linearFunction f) { Vector2 poi; if (infiniteSlope) { poi.x = b; poi.y = f.slope * b + f.b; } else if (f.infiniteSlope) { poi.x = f.b; poi.y = slope * f.b + b; } else if (f.slope == this.slope) { return(null); //If No intersection (or too many), give it nothing } else { poi.x = (f.b - this.b) / (this.slope - f.slope); poi.y = slope * poi.x + b; } Vector2?return_poi = poi; return(return_poi); }
// Update is called once per frame void Update() { if (GetComponent <Renderer> ().isVisible) { indicator.SetActive(false); indicator.GetComponent <Renderer>().enabled = false; } else { cameraPos = Camera.main.transform.position; //Set the camera position lf_indicator = new linearFunction(cameraPos, transform.position); //Draw the line //Draw our screen lines linearFunction left_screen = new linearFunction(0, -1 * cameraSize.x * 0.5f + cameraPos.x, true); linearFunction right_screen = new linearFunction(0, cameraSize.x * 0.5f + cameraPos.x, true); linearFunction top_screen = new linearFunction(0, cameraSize.y * 0.5f + cameraPos.y, false); linearFunction bottom_screen = new linearFunction(0, -1 * cameraSize.y * 0.5f + cameraPos.y, false); rotateIndicator(AngleBetweenVector2(new Vector2(0, 1), ((Vector2)transform.position - cameraPos))); //Check our intersection with all lines (if line appears in the renderer & is between our position and cameraCenter, then we found it) Vector2?poi = lf_indicator.intersectswithMe(left_screen); Debug.Log(poi.Value); if (ifIsValidIndicatorLocation(poi)) { left_screen = new linearFunction(0, -1 * cameraSize.x * 0.5f + cameraPos.x + offset_from_screen * 2, true); poi = lf_indicator.intersectswithMe(left_screen); indicator.transform.position = poi.Value; } poi = lf_indicator.intersectswithMe(right_screen); if (ifIsValidIndicatorLocation(poi)) { right_screen = new linearFunction(0, cameraSize.x * 0.5f + cameraPos.x - offset_from_screen * 2, true); poi = lf_indicator.intersectswithMe(right_screen); indicator.transform.position = poi.Value; } poi = lf_indicator.intersectswithMe(top_screen); if (ifIsValidIndicatorLocation(poi)) { top_screen = new linearFunction(0, cameraSize.y * 0.5f + cameraPos.y - offset_from_screen, false); poi = lf_indicator.intersectswithMe(top_screen); indicator.transform.position = poi.Value; } poi = lf_indicator.intersectswithMe(bottom_screen); if (ifIsValidIndicatorLocation(poi)) { bottom_screen = new linearFunction(0, -1 * cameraSize.y * 0.5f + cameraPos.y + offset_from_screen, false); poi = lf_indicator.intersectswithMe(bottom_screen); indicator.transform.position = poi.Value; } indicator.SetActive(true); indicator.GetComponent <Renderer>().enabled = true; } }