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 catwoman · Nov 16, 2012 at 02:42 PM · positionobjectgamescalescreen

how to scale an object in order to fix the screen dimensions

Hi, in my script I instantiate a Game Object, in this case a PLANE.

The dimensions of the plane should be related to the screen size. So for example the plane's width should be half the screen's width.

I'm not an expert about the dimension of object, and the "scale" function is a bit tricky.

This is my code, really simple:

         //window
         window_position = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width + 120, Screen.height / 2, 10));
         GameObject window= (GameObject)Instantiate(windowObject, window_position, Quaternion.Euler(-90, 0, 0));
         *window.transform.localScale = new Vector3(4, 1, 6*);

Can you give me some hints, please?

Thanks, I really appreciate it! :)

Comment
Add comment · Show 2
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 PaulUsul · Nov 16, 2012 at 03:24 PM 0
Share

What are you trying to do? It really depends on the distance to the plane, or is that what you want to change?

avatar image catwoman · Nov 16, 2012 at 03:37 PM 0
Share

the GameObject i'm instantiating should be as wide as half of Screen Width...

Or in a more general way, how to relate the width of an object to the Width of the Screen currently running the game...

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by catwoman · Nov 17, 2012 at 01:06 AM

any idea?

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 Hedayk · Nov 16, 2012 at 10:14 PM

I'm not exactly sure what you want but you should look into Screen.width and Screen.height .

Something like:

 window.transform.localScale = Vector3(Screen.width/2,Screen.height/2,1);

Hope that helps.

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 catwoman · Nov 17, 2012 at 09:15 AM 0
Share

I don't think this is going to work... I mean, you're comparing a Scale with a Dimension... they'are two differend measures...

I think you understood my question: i'd like to scale an object in order to fill exactly the Screen dimension.

Thanks

avatar image
0

Answer by PaulUsul · Nov 17, 2012 at 10:52 AM

You need to look at the Camera class and all the different projection matrixs. cameraToWorldMatrix and worldToCameraMatrix

It is probably going to end up being some differential and harder math.

I would fake it with the use of camera.WorldToScreenPoint and the gameObjects bounds. I have made a small project as an example, with a plane and a camera.

this is the jist of it

         // b = bounds, cam = camera
 
         // LookAt gives a forward vector towards the object.
         cam.transform.LookAt(b.center);
 
         // This is the point we use to correct after.
         point = cam.WorldToScreenPoint(go.transform.position + new Vector3(b.extents.x, 0, 0));
 
         if (correct)
         {
             Vector3 newPosition = transform.position;
             float screenCenter = Screen.width * 0.5f;
             float halfScreenWidth = screenCenter * 0.5f + Screen.width * 0.5f;
 
             // the plus 1 is so we don't jump back and forth.
             if (point.x > halfScreenWidth + 1)
             {
                 newPosition += -cam.transform.forward * Time.deltaTime * speed;
             }
             if (point.x < halfScreenWidth - 1)
             {
                 newPosition += cam.transform.forward * Time.deltaTime * speed;
             }
 
             cam.transform.position = newPosition;
         }



cameratest.zip (134.1 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 tpainton · May 17, 2015 at 07:08 PM 0
Share

With regard to this question. Everyone wants to use the camera and every question involves scaling one object, not every object in the scene.

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

11 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

Related Questions

Returning position of game object in c# 2 Answers

Collide edge of object with the edge of the screen 0 Answers

Game object position. 1 Answer

Is there a way to lock GUI buttons to a screen position insted of a pixel defined position so it will show up no matter the resolution of the screen? 0 Answers

Coincide Input.mousePosition with UI element position to be on same coordinate system 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