• 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 07, 2015 at 10:23 AM · c#raycastcolliderworldtoscreenpoint

WorldToScreenPoint returning ridiculously high number

I have a script that will raycast and take the transform.position of the collider $$anonymous$$t and another raycast on a second collider $$anonymous$$t and take their transform position to create a rect. The original code without worldtoscreenpoint works 100% fine, but as soon as I add that in, the number keeps increasing to ridiculous, near infinite numbers. I tried to figure it out but it just doesnt make any sense how one of the worldtoscreenpoint works properly and how the other worldtoscreenpoint doesn't. How do I fix t$$anonymous$$s?

 using UnityEngine;
 using System.Collections;
 
 public class TopCamView : MonoBehaviour {
 
     public Texture2D randomtexture = null;
     public Camera cameracomp;
     public static Rect selectionBox = new Rect(0,0,0,0);
     public Vector3 originalPos = -Vector3.one;
     public Vector3 currentPos = -Vector3.one;
     // Use t$$anonymous$$s for initialization
     void Start () {
         cameracomp = GetComponent<Camera>();
         //cameracomp.orthograp$$anonymous$$cSize = 
     }
     
     // Update is called once per frame
     void Update () {
         CheckCamera();
     }
     void CheckCamera() {
         if (Input.GetMouseButtonDown(1)) 
         {
             RaycastHit $$anonymous$$torg = new RaycastHit();
             Ray rayorg = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast(rayorg,out $$anonymous$$torg))
             {
                 originalPos = $$anonymous$$torg.collider.transform.position;
                 Debug.Log("raycast original: " + originalPos);
             }
         }
         else if (Input.GetMouseButtonUp(1)) 
         {
             if (selectionBox.width < 0) {
                 selectionBox.x += selectionBox.width;
                 selectionBox.width = -selectionBox.width;
             }
             if (selectionBox.height < 0) {
                 selectionBox.y += selectionBox.width;
                 selectionBox.height = -selectionBox.height;
             }
             /*originalPos = -Vector3.one;*/
         }
         if (Input.GetMouseButton(1)) {
             RaycastHit $$anonymous$$tcur = new RaycastHit();
             Ray raycur = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast(raycur,out $$anonymous$$tcur))
             {
                 currentPos = $$anonymous$$tcur.collider.transform.position;
             }
             originalPos = Camera.main.WorldToScreenPoint(originalPos); //variable that screws up
             currentPos = Camera.main.WorldToScreenPoint(currentPos); //t$$anonymous$$s one for some reason is fine
             Debug.Log(originalPos);
             //selectionBox = new Rect(originalPos.x, InvertMouseY (originalPos.y), currentPos.x - originalPos.x, InvertMouseY(currentPos.y) - InvertMouseY(originalPos.y));
             selectionBox = new Rect(originalPos.x, InvertMouseY(originalPos.y), currentPos.x - originalPos.x, InvertMouseY(currentPos.y) - InvertMouseY(originalPos.y));
         }
     }
     public static float InvertMouseY(float y) {
         return Screen.height - y;
     }
     void OnGUI()
     {
         if (originalPos != -Vector3.one)
         {
             GUI.color = new Color(1,1,1,.5f);
             GUI.DrawTexture(selectionBox,randomtexture);
         }
     }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Mouton · Jul 07, 2015 at 11:02 AM

You are self-updating the variable "originalPos" on every frame.

"Input.GetMouseButton(1)" will always be true everytime when the user press left mouse button and "originalPos" will be the result of its last value modified by the WorldToScreenPoint() method. Suppose the user press the left mouse button for 1 second and the game is running at 60 fps, "originalPos" will be modified 60 times (and will turn into incredibly $$anonymous$$gh values)

In order to prevent that, you can use Input.GetMouseButtonUp() or Input.GetMouseButtonDown() Another way would be to use a local variable (temporary variable) w$$anonymous$$ch records the WorldToScreenPoint() position.

Hope t$$anonymous$$s helps.

Comment
Add comment · Show 1 · Share
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
avatar image Lawlhwut · Jul 07, 2015 at 11:08 AM 0
Share

So that's why. I just moved it to Input.GetMouseButtonDown() and now it works. Thanks!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 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 avatar image

Related Questions

Smooth Movement on Geometry Collission? 0 Answers

Can't get a laser working properly. 2 Answers

Raycast not working on object without gravity 0 Answers

(C#) Collider to collider2d 0 Answers

Does Physics.Raycast(All) support/use Acceleration Structures internally? 0 Answers


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