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
2
Question by sriram90 · Dec 02, 2010 at 10:18 AM · movementgameobjectmouse

move Game Object(hand) with mouse movement

hi guys,

i want to move my player's hand object with mouse cursor movements...can any one help me...i have googled past some hours, still i'm struggling to find the answer...

thank you..

Comment
Add comment · Show 4
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 Proclyon · Dec 02, 2010 at 10:27 AM 0
Share

your question is posted as a code block. Could you please be so kind to post it as text? There is a previewer when posting anything that helps you show what you are posting before doing so to prevent this kind of thing (Just saying).

avatar image StephanK · Dec 02, 2010 at 11:07 AM 0
Share

Should the hand follow the exact cursor position?

avatar image sriram90 · Dec 02, 2010 at 11:15 AM 0
Share

@spree

yes, the object must follow the exact mouse position..

@procloyon

hey thx to edit that, i'll correct my mistake next time

avatar image sriram90 · Dec 02, 2010 at 11:24 AM 0
Share

it'll be good to move along with mouse cursor... because i wan to move and click some objects like search....

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Novodantis 1 · Dec 09, 2010 at 10:28 AM

You are moving a 3D world game object using a 2D input (mouse), so you need to decide how to handle the third axis. In this situation it sounds like you will want it to move on a 'virtual plane' aligned to the camera; so it is always the same distance away and moves on a 2d surface, parallel to the screen.

To do this we need to translate the cursor location from screen space (x&y on the screen) to world space (x,y,z in your game world). This is done by creating a Ray, like so:

var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);

This 'shoots' a ray from the position of the mouse, through the screen and into the game world. Well, you can think of it like that. Rays are infinite in length; what we want is a point along it. So we use:

var pos : Vector3 = ray.GetPoint(5);

This gets a 3d coordinate for a point on our ray, 5 units from the camera. You can adjust this as necessary, or better yet make it a variable.

We now have our position, so all we need to do is make the object in question use it every frame:

transform.position = pos;

Adding those three lines should be a good starting point to getting the behviour you want. Hope this helps.

Comment
Add comment · Show 2 · 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 insanetheta · Aug 30, 2012 at 08:39 PM 0
Share

thanks for posting this! I'd been stuck for hours in the mire of raycast hit testing

avatar image benbucksch · Nov 26, 2017 at 10:24 PM 0
Share

This is so awesome. I've never seen picking a 3D object using 2D mouse being that simple. Thank you for this tip, novodantis1!

avatar image
0

Answer by benbucksch · Nov 30, 2017 at 07:56 PM

With the above solution, the hand changes its height in the virtual world as you move the mouse up/down.

If instead you want to keep the hand at a stable height in the world, you can do this:

 void Update() {
     // mouse wheel y moves hand to/from ground
     this.height += Input.mouseScrollDelta.y;
     // Don't go below ground
     this.height = Mathf.Max(this.height, 0);
 
     // Keep hand under mouse pointer, at stable world height
     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     var heightPlane = new Plane(Vector3.up, new Vector3(0, this.height, 0));
     var distanceFromCamera = 0F;
     heightPlane.Raycast(ray, out distanceFromCamera);
     transform.position = ray.GetPoint(distanceFromCamera);
 }
 
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

2 People are following this question.

avatar image avatar image

Related Questions

Moving GameObjects with mouse and check contains 0 Answers

Camera movement question 1 Answer

Rotate the camera around a gameObject with a dynamic up value. 0 Answers

Moving a Game Object to the mouse 4 Answers

when mouse is on a object do this 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