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 PavGos · Jun 25, 2018 at 09:05 AM · positionvector3unity 2dx

Vector3 x start position doesn't work properly

The code is assigned to the paddle in a block breaker game.

 void Update () {
         Vector3 paddlePos = new Vector3(0f, this.transform.position.y, 0f);
 
         float mousePosInBlocks = Input.mousePosition.x / Screen.width * 12;
         print(mousePosInBlocks);
 
         paddlePos.x = mousePosInBlocks;
 
         this.transform.position = paddlePos;
     }


Whatever I set to the x value: 0f, -5f or this.transform.position.x, the paddle always starts from the left side (5f). It ignores coordinates I've inputted in unity (when there's a this.transform.position.x in a code) or this 0f coordinates I've set. How to make the paddle start in the middle (0f)?

Thanks in advance!`enter code here`

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 Harinezumi · Jun 25, 2018 at 09:50 AM 1
Share

From the scripting reference for Input.mousePosition "The bottom-left of the screen or window is at (0, 0). The top-right of the screen or window is at (Screen.width, Screen.height)."
So the calculation for mousePosInBlocks will give you 0 when the mouse is on the left, and 12 when the mouse is on the right. If you want the paddle to be in the centre when the mouse is in the centre, subtract 0.5f before you multiply with 12 : float mousePosInBlocks = (Input.mousePosition.x / Screen.width - 0.5f) * 12;

I'm not sure why the calculation doesn't affect the position, it should modify it. Is there maybe another script or component on the object that would modify the position of the paddle?

BTW, you can simplify the code to this:

 private void Update () {
     float mousePosInBlocks = ... // do here your calculation
     print (mousePosInBlocks);
     transform.position = new Vector3(mousePosInBlocks, transform.position.y, 0);
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by hectorux · Jun 25, 2018 at 11:48 AM

Try this (it will be more eficent, and also i think it solves your problem):

      public float screeonOfssetWithWorld;

       void Update () {
           
          float mousePosInBlocks = Input.mousePosition.x / Screen.width * 12-screeonOfssetWithWorld;
          print(mousePosInBlocks);
  
          paddlePos.x = mousePosInBlocks;
  
          this.transform.position = new Vector3(mousePosInBlocks,  this.transform.position.y, 0f);
      }

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

122 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

Related Questions

How to detect if transform.position is not vector3 + 100 on x position 1 Answer

Need some help with to grap .x with a Raycasthit (c#) 1 Answer

Position in Vector (C#) 1 Answer

Cannot implicitly convert type 'int' to 'UnityEngine.Vector3' 3 Answers

Unity 2D Position Issues 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