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 /
  • Help Room /
avatar image
0
Question by ezaz12121 · Nov 11, 2015 at 01:07 AM · script.input.mousepositionscreen.width

Moving GameObject with Mouse problem

my camera is at 0,0,0 as you can see in the script below, my mouseInput.x / ScreenWidth * 16 statement, i used the print method to see my mouse and see how big my screen play space is and it is 16 across with my mouse , my problem is when i hit play , my paddle starts of at the end of the right side(position 8) and only goes to the middle of the screen , when i move my mouse to the middle of the screen its at position 0 when i move the camera over half way to the right , it works perfectly (so it starts at 0), is there a reason why my paddle starts at the end of the right side instead of the middle. am i doing this wrong ? When i move the paddle with the arrow keys i dont have this problem. i already have a way to find out the left and right side of the camera and i have them in a variable called xmin and xmax and using it to restrict the paddle in between those points, i do not know what am i doing wrong. i am completely lost been at it for days. Also when i remove the Screen.Width / 16 from float MousePos = Input.mousePosition.x/ Screen.width * 16; my paddle is placed out of screen to 480 on x posistion , so that did not work either. thank you i think i need to have it so it starts at what ever position its at right?

 using UnityEngine;
 using System.Collections;
 
 public class Paddle : MonoBehaviour {
 
     public float Speed = 1f; 
 
 
 
     float xmin;
     float xmax;
     float padding = 1f;
     // Update is called once per frame
     
     void Start(){
 
         // get Right and left side of the camera
         float distance = transform.position.z - Camera.main.transform.position.z;
         Vector3 leftmost = Camera.main.ViewportToWorldPoint(new Vector3 (0, 0, distance));
         Vector3 rightmost = Camera.main.ViewportToWorldPoint(new Vector3(1,0,distance));
          xmin = leftmost.x + padding;
         xmax = rightmost.x - padding;
     }
     
     
     
     void Update () {
     
         print(Input.mousePosition.x/ Screen.width * 16);
         Movement ();
         
     }
     
 
     
     
     
     void Movement(){
 
         //get mouse inout
 
         float MousePos = Input.mousePosition.x/ Screen.width * 16;
     //    //constrain paddle to the right and left edge of camera.
         float newX = Mathf.Clamp (MousePos, xmin, xmax);
 
 
         this.transform.position = new Vector3 (newX
                                         , transform.position.y, transform.position.z);
     }
     
     
     
 
     
 }
 







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 AngryTilde · Nov 11, 2015 at 02:34 AM

Since your camera is at 0,0,0 the left side of the view port is negative (in worldSpace), and as Screen size and Mouse Position start at 0 on the left hand side rather then the center you'll never see your paddle moving before the center point of your screen.

You can either A) ensure your cameras left most view x point is 0 or b) modify your script to account for the screen starting at 0 with something like this:

 float offsetMousePos = (Input.mousePosition.x -  (Screen.width * 0.5));

which would return the Mouse X relative to the mid point of the screen.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Animation only plays in one of the objects 1 Answer

Load scene after battle 0 Answers

Action (by collider) 0 Answers

Use c# "ref" with a GameObject or Component 1 Answer

How to set a text to a inputfield in the editor? 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