• 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 /
  • Help Room /
avatar image
0
Question by Philip-ACN · Aug 02, 2018 at 03:57 PM · 2d sprites

2D element displaying onscreen in wrong place. Why?

Hello, Apologies for the long post, bear with me!

I'm trying to make a minimap for a racing game but I'm having trouble getting the 2d element to appear onscreen in the correct place.

Here is the script I'm using to determine the correct place onscreen for the 2d element. (Hereby called playerLocation)

     public GameObject minimapImage;
     public Vector2 playerMinPos = new Vector2( -500f, -30f), playerMaxPos = new Vector2(1500f, 130f), uiMinPos = new Vector2(-400f, -30f), uiMaxPos = new Vector2(400f, 40f);
 
     public float playerXRange, playerYRange, playerXNormal, playerYNormal, uiXRange, uiYRange;
 
     public float currentX, currentZ;
 
     public Vector3 uiPosition;
 
     // Use this for initialization
     void Start () {
         playerXRange = playerMaxPos.x - playerMinPos.x;
         playerYRange = playerMaxPos.y - playerMinPos.y;
 
         uiXRange = uiMaxPos.x - uiMinPos.x;
         uiYRange = uiMaxPos.y - uiMinPos.y;
     }
 
     // Update is called once per frame
     void Update () {
         playerXRange = playerMaxPos.x - playerMinPos.x;
         playerYRange = playerMaxPos.y - playerMinPos.y;
 
 
         currentX = gameObject.transform.localPosition.x;
         currentZ = gameObject.transform.localPosition.z;
 
         playerXNormal = (currentX - playerMinPos.x ) / playerXRange;
         playerYNormal = (currentZ - playerMinPos.y ) / playerYRange;
 
 
         uiPosition  = new Vector3( uiMinPos.x + ( uiXRange * playerXNormal ),
                                    uiMinPos.y + ( uiYRange * playerYNormal ) + 10f,
                                    0f );
         minimapImage.transform.localPosition = uiPosition;
     }

minimapImage is the minimap player location sprite.

playerMinPos/playerMaxPos Is the top left, and bottom right of the players allowed area in world space.

uiMinPos/uiMaxPos the same as above but the UI top left/bottom right in screen space.

playerXRange/playerYRange is calculated from the min/max positions as the width/height of the playable area.

playerXNormal/playerYNormal is the normalised values (from 0 to 1) used to determine the UI position from the UI Range.

uiXRange/uiYRange as the player range, but for the UI.

currentX/currentZ currently only public to see what values are set as the player location.

uiPosition currently only public to see what values are set as the UI location.

.

OK then, based on the above, the formula seems to work. When moving the player all the way to the left, uiPosition.x reads as -400 as it should, and when all the way to the right, it reads as 400, again, as it should.

.

However, when looking at the inspector for the playerLocation object, it reads as a different value. (For example, when uiPosition.x reads as 332.7953, the gameObject position reads as 317.6499. (-396.8521 reads as -460.6353 and so on)

.

Everything is being shifted over to the left, but if I add say 40f to the x position, its correct towards the right of the minimap, but till wrong towards the left.

.

What am I doing wrong!!

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

Answer by Philip-ACN · Aug 03, 2018 at 08:45 AM

Never mind. Today when running the editor, it all worked as expected.

I have no idea why the same code wouldn't work one day, then work the next.

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.

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

153 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 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 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 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 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 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

Poor quality sprite 2D game 0 Answers

How to efficiently reduce size of spritesheets in 2d games. 0 Answers

How can I spawn a game object/prefab on my mouse pointer while only hovering over a specific area? 1 Answer

How to get the amount of light shining on a sprite?,How to get the amount of light shining on a Sprite? 0 Answers

About Simple Left and Right movement with this CODE 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