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 Bessu · Dec 22, 2014 at 04:16 PM · transformpositionrect

Rect Transform local position to gameobject position

I get the local position within a rect transform and save it for later use.

// Rect transform bounds RectTransform rt = gameObject.GetComponent ();

// Localpoint RectTransformUtility.ScreenPointToLocalPointInRectangle(rt, mousepos, Camera.main, out output); Vector2 localpoint = output;

Now I would like to use this local-point output to instantiate another gameobject to this location but I can't figure how to convert it for gameObject.position so that the game object would appear in the same spot as where the mouse-click was.

Comment
Add comment · Show 1
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 RedHedZed · Dec 22, 2014 at 07:17 PM 0
Share

I'm pretty sure that you can use ScreenToWorldPoint to convert UI coordinates to world coordinates. I'm doing this in a 2D game, and it's working very well. It may not work quite as well in a 3D game since Screen coordinates do not have a Z-Value.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Mmmpies · Dec 25, 2014 at 11:30 PM

@RedHedZed is right but I think it'll work differently in 3D. The test I did for another question is this:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class RectUtil : MonoBehaviour {
 
     public Camera MainCam;
     public RectTransform myRectT;
     private Vector3 result;
 
 
     
     //ScreenPointToWorldPointInRectangle(rect: RectTransform, screenPoint: Vector2, cam: Camera, worldPoint: Vector3): bool;
 
     // Use this for initialization
     void Start () {
         Vector2 myV2 = new Vector2(0,24);
         Debug.Log (RectTransformUtility.ScreenPointToWorldPointInRectangle(myRectT,
                                                                            myV2,
                                                                            MainCam,
                                                                            out result));
         Debug.Log (result);
     }
 }

the output returns 0 on the Z. I haven't tried this but I suspect you'll need to record the camera transform before going into the new UI and then use the results to add to that transform.

You'll also need to deal with depth but test the output of that code.

Switch from 3D to 4.6 GUI and in 3D move around and see if the values change for that (randomly selected) 0,24 Vector2 when you go into the menu.

If it does (significantly) then movement in 3D makes a difference, if not then recording the camera transform before going into the 4.6 menu and use the output information to add to that transform.

That came out a lot more complicated than I intended. Hope you understand!

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Convert Input.mousePosition to RectTransform pivot position 2 Answers

transform.position 3 Answers

Difference between transform.forward and transform.position.z? 1 Answer

Storing transforms from objects in Array 1 Answer

Place a object in front the camera in the coordinate 0 of Y axis 2 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