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 nivenj · Apr 18, 2013 at 01:44 PM · cameragameobjecttexture2dlocalposition

Object Placement alignment to background image

Hi,

I've been struggling with this for a week now. I'm new to unity and the learning curve is steep but enjoyable :-)

What I am trying to achieve is this:-

I have an background image which will act as a playing board of sorts (think chess board). This image will be static during play. I want this image to scale to the full screen of whatever mobile device is in use.

I place my objects using localPosition which align to a specific area on the background image. the problem is, that if I view it on a 16:9 screen the objects are aligned differently than when i view it on a 16:10 screen. I understand why this is, as i'm positioning based on a fixed point in space but the image is scaling.

How can I go about achieving what I am trying to do. i.e. perfect alignment irrespective of screen scaling.

Thanks in advance

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 robertbu · Apr 18, 2013 at 02:29 PM 0
Share

Rather than resizing the image, consider moving the camera or change the camera field of view (or orthographic size) in order to have the image fill the screen.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by alexanderbrevig · Apr 18, 2013 at 04:31 PM

You could do either of the following:

  1. Create a mathematical model of the board, so that you could simply calculate the position based on some data. F.ex if you wanted to be at the beginning of the fourth square of a chess board you could do this: float xPos = (4-1)/8 * Screen.width;

  2. If you know the pixel position of where you want to be you can simply map to screen coords mathematically: screenPosX = Screen.width / image.width * imagePosX. After you know screen x and y you can use Camera.ScreenToWorldPoint for generating a world coordinate from the screen coords.

Good luck!

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

Answer by nivenj · Apr 18, 2013 at 08:02 PM

Thanks for the response guys,

@robertu taking your advice I found the following post auto-scale

 public class PositionCamera : MonoBehaviour {
  
     public float fWidth = 9.0f;  // Desired width 
  
     void Start () {
  
        float fT = fWidth / Screen.width * Screen.height;
        fT = fT / (2.0f * Mathf.Tan (0.5f * Camera.main.fieldOfView * Mathf.Deg2Rad));
        Vector3 v3T = Camera.main.transform.position;
        v3T.z = -fT;
        transform.position = v3T;
     }

This works perfectly if the camera is at a 90 degree angle to the image, the problem is, my Camera POV is at an angle to the Image to give the perception of depth.

Any idea how i could reconfigure the above code for my situation? (I'm a bit lost at how it works).

This is the image:

alt text

This is the Camera POV: alt text


background_template.png (20.7 kB)
pov.png (54.0 kB)
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 nivenj · Apr 18, 2013 at 09:00 PM 0
Share

EDIT: I managed to work it out, I had to adjust the y value by a similar amount as the z and it works like a charm. Thanks for the steer in the right direction robert;

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

12 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

Related Questions

Rendertexture to texture2d in Unity5 0 Answers

How can i change a childs local position with a variable? 1 Answer

Is it possible to create a texture from a static image from a camera without using CaptureScreenshot or RenderToCubemap 1 Answer

Unity3D scene not rendering objects in Game screen 3 Answers

Moving a canvas relative to a gameobject's position 1 Answer


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