Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 solarsaenz · Feb 08, 2019 at 01:46 AM · 2d gamepositioning

Positioning Objects in a 2D Project

I am making a 2D pong game. The only thing that is eluding is initially placing the paddles on the screen. My intention is to place them just a little bit away from the right and left bounds of the camera. When I initially start my game, they are in a funky position. alt text

I have a game over screen with a replay option. When I lose and do the replay, the paddles are exactly where I want them. alt text

Here is my code for figuring out the positions of each paddle.

Right paddle (in the Start function):

 bottomLeft = Camera.main.ScreenToWorldPoint(new Vector2(0, 0));
 topRight = Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, Screen.height));
  Debug.Log("TopRightX: " + GameManager.topRight.x.ToString()); 
  Debug.Log("TopRighty: " + GameManager.topRight.y.ToString());
  Vector 2 pos = Vector2.zero;
  pos = new Vector2(topRight.x, 0);
  pos -= Vector2.right * transform.localScale.x;
  transform.position = pos;
 Debug.Log("rPaddlePosition: " + transform.position.ToString());

Left paddle (in the Start function):

 Debug.Log("BotttomLeftX: " + bottomLeft.x.ToString());
 Debug.Log("BotttomLeftY: " + bottomLeft.y.ToString());
 pos = new Vector2(bottomLeft.x, 0);
 pos += Vector2.right * transform.localScale.x;
 transform.position = pos;
 Debug.Log("lPaddlePosition: " + transform.position.ToString());
     

These are the outputs on the first time loading.

 TopRightX: 0
 TopRighty: 0
 rPaddlePosition: (-0.3, 0.0, 0.0)
 BotttomLeftX: -10.46591
 BotttomLeftY: -5
 lPaddlePosition: (-10.1, 0.0, 0.0)

These are the outputs after I go through the game over screen and replay the game.

 TopRightX: 10.46591
 TopRighty: 5
 rPaddlePosition: (10.1, 0.0, 0.0)
 BotttomLeftX: -10.46591
 BotttomLeftY: -5
 lPaddlePosition: (-10.1, 0.0, 0.0)

My replay functionality is just a

 SceneManager.LoadScene(0); 

Where the scene index of the "game play scene" is 0. I'd appreciate any insight into getting the paddles placed correctly the first time.

bad.jpg (24.2 kB)
good.jpg (25.3 kB)
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 Adam_g · Feb 08, 2019 at 02:41 AM 0
Share

Judging by the outputs, it looks like the view was square when the values were calculated. Do you have "$$anonymous$$aximise on Play" checked off in the game window? The values could match the dimensions of the game view in edit mode. Can you add a screenshot of your editor layout?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by James_BadAlchemy · Feb 08, 2019 at 03:10 AM

I ran into a lot of the same issues as you doing it that way. It's better, if you haven't already, to use the Canvas system unity offers. It has built-in functionality for stretching, filling, padding, ratios, etc. You just gotta play around with it.

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 solarsaenz · Mar 07, 2019 at 12:47 AM

For some reason, when I updated my Unity, this just "automagically" started working for me. Not sure if i ran into a Unity bug, or if Unity just was optimized in some way to account for whatever I was doing. Thank you to those who offered a response.

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

115 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

Related Questions

Make an enemy follows and point towards player 1 Answer

i need help about players hands positions in 2D multiplayer card game 0 Answers

How Do I: Make a 2D sprite that is bouncing move horizontally relative to the position of the platform? 0 Answers

How to prevent overlapping UI 0 Answers

My game wont run and my code is correct 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