- Home /
Rect from two points issue
I have a script that will draw a Rect from the center of two objects that are detected by raycast. If the center of the game object is within the box, then it will highlight it red. The box draws nicely and the objects get highlighted except for the ones in the bottom side and the right side. No matter which direction I make the box in (+x, +y; +x,-y etc) the bottom side and the right side are never in the box. I don't know how to fix this. What can I do?
Rectangle script: using UnityEngine; using System.Collections;
public class TopCamView : MonoBehaviour {
public Texture2D randomtexture = null;
public static Rect selectionBox = new Rect(0,0,0,0);
public Vector3 originalPos = -Vector3.one;
public Vector3 currentPos = -Vector3.one;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
CheckCamera();
}
void CheckCamera() {
//Getting firstpoint
if (Input.GetMouseButtonDown(1))
{
RaycastHit hitorg = new RaycastHit();
Ray rayorg = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(rayorg,out hitorg))
{
if (hitorg.collider != null)
{
originalPos = hitorg.collider.transform.position;
originalPos = Camera.main.WorldToScreenPoint(originalPos);
}
}
}
//Flipping rect if not +x,+y
else if (Input.GetMouseButtonUp(1))
{
if (selectionBox.width < 0) {
selectionBox.x += selectionBox.width;
selectionBox.width = -selectionBox.width;
}
if (selectionBox.height < 0) {
selectionBox.y += selectionBox.height;
selectionBox.height = -selectionBox.height;
}
}
//Getting second point (current mouse cursor)
if (Input.GetMouseButton(1)) {
RaycastHit hitcur = new RaycastHit();
Ray raycur = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(raycur,out hitcur))
{
Debug.Log(hitcur.collider.tag);
if (hitcur.collider.tag == "GridUnit")
{
currentPos = hitcur.collider.transform.position;
currentPos = Camera.main.WorldToScreenPoint(currentPos);
}
else
{
currentPos = originalPos;
Debug.Log(currentPos);
currentPos = Camera.main.WorldToScreenPoint(currentPos);
}
}
float xdir = currentPos.x - originalPos.x;
float ydir = InvertMouseY(currentPos.y) - InvertMouseY(originalPos.y);
//Drawing rect
selectionBox = new Rect(originalPos.x, InvertMouseY(originalPos.y), xdir, ydir);
}
}
public static float InvertMouseY(float y) {
return Screen.height - y;
}
void OnGUI()
{
if (originalPos != -Vector3.one)
{
GUI.color = new Color(1,1,1,.3f);
GUI.DrawTexture(selectionBox,randomtexture);
}
}
}
Highlight object function attached to the objects:
void boxSelect()
{
if (rend.isVisible && Input.GetMouseButtonUp(1))
{
Vector3 camPos = Camera.main.WorldToScreenPoint(transform.position);
camPos.y = TopCamView.InvertMouseY(camPos.y);
selectedBox = TopCamView.selectionBox.Contains(camPos);
}
if (selectedBox)
{
rend.material.color = Color.red;
}
else if(!selectedBox)
{
rend.material.color = Color.gray;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220611230314im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
(C#) Collider to collider2d 0 Answers
[c#] Hit a object and shoot it away with raycast? 2 Answers
Raycast2D Layermasks and incomplete docs 1 Answer
WorldToScreenPoint returning ridiculously high number 1 Answer
Multiple Cars not working 1 Answer