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 AlexGee33 · Sep 28, 2011 at 12:35 AM · beginnerstuckinput.mouseposition

Can't move game object left of center

I'm new (obviously...) and I'm trying to use the mouse to control my game object's X position while running the following code:

transform.position.x = Input.mousePosition.x;

When my mouse is at the left edge of the screen the corresponding game object will stay at the center, if I move the cursor to the right I can move the game object to the right and then back to the center, but regardless how far to the left my cursor is on the screen the game object will not move past the center. After adjusting the "Game" drop down selection menu from "Free Aspect" to "Standalone" the game object will move freely from left to right but the numbers in the debug log are not accurately depicting the center of the screen as 0.

What do I have to do to get the game object to move properly to the left side of the screen while in "Free Aspect"? Also please note that I'm using a laptop with a finger mouse in case the mouse type has anything to do with it.

Thanks,

Alex

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Sep 28, 2011 at 01:06 AM

You are messing things: the screen is a 2D space whose coordinates range from (0,0) to (Screen.width, Screen.height), while the objects are in a 3D world where the coordinate system is totally different (it depends on the camera position and orientation).
If you want to drag the object with the mouse, you must convert from the screen space to the world space - but since the world is 3D, the mouse position must be extended to 3D by adding the distance from the camera as the Z coordinate:

var dist: float;

function OnMouseOver(){ // if this object clicked, save the distance from the camera in dist if (Input.GetMouseButtonDown(0)){ dist = Vector3.Distance(transform.position, Camera.main.transform.position); } }

function Update(){ // while the button remain pressed, drag the object if (Input.GetMouseButton(0)){ var mousePos: Vector3 = Input.mousePosition; mousePos.z = dist; // extend mousePosition to 3D with the distance var pos = Camera.main.ScreenToWorldPoint(mousePos); // convert to 3D world transform.position = pos; // follow the mouse pointer } }

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 PatrikNyblad_SI · Oct 14, 2011 at 07:17 AM

This piece of code will check to see if you press the left mouse button and when you do it will cast a ray from the coordinates (pixels) in you gamewindow. If this ray hits an object like a plane with a collider attached (you have to have some sort of ground like a plane for this to work) it will move the current objects position.x to the hits position.x.

 if (Input.GetMouseButtonDown(0)) {
     var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     var hit: RaycastHit;
     Debug.DrawRay (ray.origin, ray.direction * 10, Color.red);

     if (Physics.Raycast(ray, hit)) {
         transform.position.x = hit.position.x;
     }
 }

Hope it helps!

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

I am stuck in tutorial 9/12 Activate the elevator 0 Answers

Are there any in depth beginner tutorials? 6 Answers

Essential math for games 2 Answers

New to Unity and stuck!!! 1 Answer

UI color issue 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