- Home /
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!
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.
Answer by mlavik1 · Aug 29, 2016 at 01:43 PM
You need to:
Find the camera position
Find the mouse click direction
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.