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 enorm · Dec 08, 2014 at 02:07 PM · transform2d gamepositionmouse-drag

Updating localPosition using parents' position

Currently I have this piece of code that update the position of a sprite on the screen when I drag it with the mouse. I want another object (the arrow) to follow it exactly at one specific point (the feet of the sprite, the middle of the bottom border of the collider), but this script doesnt work in fullscreen mode as it does in window mode because of differing scales ( I think ). So in order to get it working I need to use to localPosition I suppose, but I have no idea what to do here.

As you can see , I've set the offset in the arrow position to 60f but this isnt relative to screen size it seems.

     void OnMouseDown()
     {
         screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
         
         offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
     }
     void OnMouseDrag()
     {
         if(!colliding){
         
         Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
     
         Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
         Vector3 curArrowToScreenPoint = new Vector3(curPosition.x, curPosition.y**-60f**,screenPoint.z);
         Vector3 curArrowPosition = Camera.main.ScreenToWorldPoint(curArrowToScreenPoint)+ offset;
             transform.position = curPosition;
             arrow.transform.position = curArrowPosition;
 
         }
         
     }
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

2 Replies

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

Answer by EvilTak · Dec 08, 2014 at 03:04 PM

Make the arrow a child of the sprite, position it exactly where you want and move only the sprite from your script. The arrow will stay at the same position relative to the sprite, i.e. it will move along with it the way you want.

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 enorm · Dec 08, 2014 at 07:16 PM 0
Share

Thank you.

avatar image
0

Answer by taxvi · Dec 08, 2014 at 03:06 PM

why don't you change line 17 like this:

 arrow.transform.position = transform.position + new Vector3(0, -1, 0);

and instead of -1 put the number you need. should work in all the resolutions.

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

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

Related Questions

Getting to move object with mouse position? 0 Answers

Adding a value to transform.position.y runs very untrusted 0 Answers

How to make object follow y-axis of another object. 1 Answer

2D RPG boomerang instantiation help. 2 Answers

Unity3D - Playback object array of position (with dynamic velocity) 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