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 user-3199 (google) · Jun 22, 2010 at 02:10 PM · movementobjectmouseclick

Moving an object to the location of a mouse click...

I've created a simple script that allows me to move the object the script is attached to to the coordinates of the mouse. However, when I began to click around, the object wouldn't go to the exact point of the mouse click but instead followed it at a distance. I'm kind of stumped on this problem, so I'll just provide the script and hope someone here knows a better way to do what I'm trying to do.

function Update () { if (Input.GetButtonDown ("Fire1")) { transform.position = Vector3(Input.mousePosition.x, 0, Input.mousePosition.y); } }

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 Jammer3000 · Dec 19, 2013 at 02:25 PM 0
Share

I want to use your script but could you tell me what mousePosition is please and thanks (:

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Tetrad · Jun 22, 2010 at 02:32 PM

Use this: http://unity3d.com/support/documentation/ScriptReference/Camera.ScreenPointToRay.html

plus this: http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html

Screen position (i.e. Input.mousePosition) don't translate 1-to-1 to world coordinates (i.e. what transform.position is).

There are a lot of questions surrounding this here. Try this http://answers.unity3d.com/search?q=drag

Comment
Add comment · Show 3 · 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 user-3199 (google) · Jun 24, 2010 at 04:45 PM 0
Share

Ok, thanks for getting back to me. I've modified my script using your info and some ideas from another script I found here. I used Raycasthit to place the object, but now I can't figure out how to constrain it to the x and z axis. Is it possible to do some using raycasting?

avatar image user-3199 (google) · Jun 24, 2010 at 04:45 PM 0
Share

Sorry, do that using raycasting?

avatar image Tetrad · Jun 24, 2010 at 06:46 PM 0
Share

Well when you do your raycast, you'll end up with a point that it hit. Just take that point (a Vector3), and set the y value to whatever your object's current y value is before setting that point to your position.

avatar image
0

Answer by Calesurf · Jun 14, 2014 at 12:25 AM

The bottom corner of the screen is 0,0 and if you click in the corn the object would go to 0,0. If you click anywhere else it would go to some crazy number. I had the same problem still haven't figured it out.

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

How To Detect MouseClick Using Raycast Java Script 0 Answers

Mouse clicking on an object (Raycast.hit) for another object to connect to it? 0 Answers

How do i make a mouse look script? 0 Answers

How do I go about moving an object side to side using the mouse. 2 Answers

Mirroring Object Movement with Mouse Position 2 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