Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 DMCH · Feb 08, 2013 at 04:09 PM · instantiateprefabmousepositiongui.matrix

Instantiating Prefab at MousePosition & GUI.Matrix

Hello,

I was able to get an object to instantiate at the mouse position correctly before I added a GUI Matrix to keep the GUI scale. This is the code I was using.

             Screen.showCursor = false;
             Rect pos = new Rect(mousePos.x, Screen.height - mousePos.y ,turretCanPlace.width, turretCanPlace.height);
             GUI.Label(pos, mineCanPlace);    
             
             // If the player pressed LMB, instatiate the turret
             if(Input.GetMouseButton(0))
             {
                 Vector3 worldPos = Camera.main.ScreenToWorldPoint(mousePos);
                 worldPos.y -= 65.8f;
                 Debug.Log("TurretSceneScript.OnGui(Placing turret) World Pos: " + worldPos);
                 Instantiate(turretPrefab, worldPos, Quaternion.identity); //AngleAxis(90, Vector3.up));
                 
                 // Set the cursor back to visible
                 Screen.showCursor = true;
             }

When I added the GUI Matrix, the prefab no longer instantiated at the correct position. Here is the code for the GUI.Matrix. It's resizing from 1920 * 1080.

 public static void AutoResize(int screenWidth, int screenHeight)
     {
         Vector2 resizeRatio = new Vector2((float)Screen.width / screenWidth, (float)Screen.height / screenHeight);
         GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, new Vector3(resizeRatio.x, resizeRatio.y, 1.0f));
     }


I messed around with the code, and got a partial fix, but as I move the cursor to the right, or down, the distance between the icon and instantiated prefab increases. Here's what I'm currently using:

 Rect pos = new Rect(mousePos.x * (1920/Screen.width), (1080/Screen.height) * (Screen.height - mousePos.y) ,turretCanPlace.width, turretCanPlace.height);

Can anyone help me figure out how to get the prefab to instantiate on the correct location? Thanks for your help!

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
0
Best Answer

Answer by DMCH · Feb 08, 2013 at 05:41 PM

After another hour of head scratching I figured it out. Started outputing the position of the label for the turret, and the mouse position. Eventually realised that the inaccuracy resulted from dividing an int by a float.

 Rect pos = new Rect(mousePos.x * (1920/Screen.width), (1080/Screen.height) * (Screen.height - mousePos.y) ,turretCanPlace.width, turretCanPlace.height);

I just changed the 1920 and 1080 to floats and everything is working ok again.

 Rect pos = new Rect(mousePos.x * (1920f/Screen.width), (1080f/Screen.height) * (Screen.height - mousePos.y) ,turretCanPlace.width, turretCanPlace.height);


Hopefully will be of some help to the next guy!

Comment
Add comment · Show 2 · 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 Flik15 · Mar 20, 2014 at 05:49 PM 0
Share

Hi D$$anonymous$$CH, I was having the exact same problem as you, and your solution helped fix it. Thanks for your help and co$$anonymous$$g back to answer your question!

avatar image DMCH · Mar 21, 2014 at 03:47 PM 0
Share

Glad I could help!

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

10 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

Related Questions

Associate objects to a prefab 1 Answer

instantiating vertically 2 Answers

How to Add Y Axis Offset to transform.localPosition on an Instantiated Prefab? 2 Answers

Referencing gameObject from script after Instantiate 0 Answers

Instantiating one prefab with different materials in one frame 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