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 /
This question was closed Jan 22, 2015 at 10:56 AM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by zenobios · Jan 18, 2015 at 10:23 PM · gui2d platformer

How to calculate correct screen coordinates for UI elements?

Hi,

I'm quite new to Unity (but an experiences Java developer) and I'm currently trying to find out how to calculate some coordinates. What I basically want to achieve is very similar to Unitys slider component (http://docs.unity3d.com/Manual/script-Slider.html). My hierarchy looks like something like this:

 - Canvas
  - Sliderline (Image)
   - SliderKnob (Image)

In a first step I simple want to be able to move the knob using the mouse along a vertical positioned line (Slideline). In case the user drags the mouse above/below the line, the knob should just stop at the top/bottom.

My problem is that I cannot figure out how to correctly convert Input.mousePosition to calculate the new position of the knob. Here is a snippet of what I'm doing currently:

 float yOnLine = Input.mousePosition.y;
 
 if (yOnLine > knobMax) {
   yOnLine = knobMax;
   Debug.Log ("Max position reached");
 } else if (yOnLine < knobMin) {
   yOnLine = knobMin;
   Debug.Log ("Min position reached");
 } else {
   yOnLine -= knobMin;
 }

where knobMax and knobMin are defined as:

 knobMax = sliderline.offsetMin.y + sliderline.rect.height;
 knobMin = sliderline.offsetMin.y

If I'm using calculations like above, the knob does move - but really weired ;).

Any tips?

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 Chris333 · Jan 21, 2015 at 04:44 PM 0
Share

Hi, take a look into the methods for converting coordinates. $$anonymous$$g. http://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html $$anonymous$$aybe that could help.

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by Noah Dyer · Jan 22, 2015 at 12:41 AM

http://unity3d.com/learn/tutorials/modules/intermediate/live-training-archive/panels-panes-windows

This isn't specifically about sliders, but will show you a better way of handling the drag events, etc.

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 zenobios · Jan 22, 2015 at 10:54 AM 0
Share

Noah,

thanks for the link. Indeed, the solution is to use

 RectTransformUtility.ScreenPointToLocalPointInRectangle (rectTransform, data.position, data.pressEventCamera, out previousPointerPosition);

to get the desired position.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Rocket launcher GUI problems 1 Answer

NullReferenceException: Object reference not set to an instance of an object PlayerController.Update () (at Assets/Scripts/PlayerController.cs:19) Please Help 2 Answers

Detect Text in GUI; Print 1 Answer

GUIDrawTexture Based Crosshairs 0 Answers

Unity editor GUI, how to prevent "passthrough"/"clickthrough" etc? 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