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 IndieBeard · Aug 28, 2016 at 10:46 AM · movement3dcontrolselecting

Making a hand move with the mouse

Hey everyone, first time posting so sorry if I'm in the wrong spot. I'm trying to make a part of a game that involves moving a hand over a table in a 3D environment with the mouse similar to how Surgeon Simulator's controls operate. (Minus the finger movements). I'm pretty new to working in 3D and I've tried a few different ways I thought would make this work but I'm completely lost. If anyone could help point me in the right direction, I would really appreciate it,

Thanks!

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 hoogemast · Aug 29, 2016 at 08:47 AM 1
Share

Hi can you clear things up. There are 2 ways i can read this particular question. In the first way you would like the mouse cursor to be the hand and it directly moves together with your mouse. That is something that can be done by getting the mousecoordinates constantly and moving your gameobject (the hand) directly to that point. the code would look something like this;

 void Update
         {
             Vector2 moveVector = new Vector2(-Input.GetAxis("$$anonymous$$ouse X"), -Input.GetAxis("$$anonymous$$ouse Y"));
             gameObject.transform.position = moveVector;
         }

there might be some conversion factors neccesary to make sure its location is right.

Secondly you can move the hand relatively to the mouse movement. So you can move the mouse and it will translate the coordinates of the mouse to the coordinates on your table. This can be done again by getting the x and y of the mouse, only this time you have to convert the values to your x/y of the table. Something like this should work:

 void Update
         {
             Vector2 moveVector = new Vector2(-Input.GetAxis("$$anonymous$$ouse X"), -Input.GetAxis("$$anonymous$$ouse Y"));
             gameObject.transform.position = moveVector * new Vector2(maxvaluex, maxvaluey);
         }

in the code the maxvaluex and maxvaluey are the maximum x and y values of your table. $$anonymous$$ake sure you move it the right way (in this code example i used y directly to the y value of the object, but you could move it over the z axis as well)

Hope this lights up the right path for you, if you have any questions feel free to ask it again.

P.S. You can always ask anything in the unity answer section, but try to use the right topics, this increases your chance of getting it answered.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by mlavik1 · Aug 29, 2016 at 01:43 PM

You need to:

  1. Find the camera position

  2. Find the mouse click direction

  3. Find unknown variable t in the formula: camPos + t*dir = intersectionPoint

If you define your table to be in the XY-plane, you will know the z-value of the intersectionPoint. If the table is positioned at z=0, you get: camPos.z = t*dir.z = 0, which makes the caluclation easy.

Code, with comments:

 using UnityEngine;
 using System.Collections;
 
 public class MouseMovement : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
 
         // We set our "table" to be at the XY-plane.
         float tableZ = 0.0f; // Our "table" is positoned at Z = 0. You may change this.
 
         // https://docs.unity3d.com/ScriptReference/Input-mousePosition.html
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
         Vector3 camPos = ray.origin;
         Vector3 mouseDir = ray.direction; // direction from camera centre towards mouse position.
                                           // .. along this vector, every point is visibly hidden behind the mouse.
 
 
         // We have origin "camPos" and direction "mouseDir". We want to find the point where this ray intersects the XY-plane at Z = tableZ
         // camPos + t*mouseDir = intersPos,  where t and intersPos are unknown
         // We know that intersPos.z = tableZ
         // => camPos.z + t*mouseDir.z = tableZ
         // => t = (tableZ - camPos.z) / mouseDir.z
         float t = (tableZ - camPos.z) / mouseDir.z;
         Vector3 intersPos = camPos + t*mouseDir; // original formula: camPos + t*mouseDir = intersPos
 
         transform.position = intersPos;
 
     }
 }
 

Here is a link to my test project: http://s000.tinyupload.com/index.php?file_id=87544764756686637826 I made it using Unity version 5.3.3

Note: You may change the value of tableZ, depending on where your table is positioned. Also, if your table is not positioned at the XY-plane, you only change the formula slightly, by using the x-value or y-value instead.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make camera position relative to a specific target. 1 Answer

Player Control Script Makes Movement Buggy? 1 Answer

Player gets stuck into object when it collides with it 0 Answers

Standard FPS Controller - Smoothing Movement 2 Answers

Locking movement to one axis only (3D game) 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