Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Lawlhwut · Jul 08, 2015 at 09:13 AM · c#raycastcollidergridrect

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;
         }
     }

alt text

whathappens.png (14.2 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges