- Home /
Trying to move an object to Mouse x and z positions only?
Hi, I'm trying to move an object in the game to the Mouse x and z coordinates whenever I right-click, but not to the Y coordinate. Here is my code:
using UnityEngine;
using System.Collections;
public class Mousetargeting : MonoBehaviour
{ void Update ()
{
if(Input.GetMouseButtonDown(1))
{
transform.position = new Vector3(mousePosition.x, -.45, mousePosition.z);
}
}
}
I'm not sure how to just call up the mouse X and Z coordinates when I'm transforming the position. Help?
Answer by robertbu · Apr 23, 2014 at 08:12 AM
Your question lacks details for a solid answer. While Input.mousePosition is a Vector3, the 'z' is 0.0, so there is no inherit 'z' to mousePosition. Depending on the rotation of the camera and the nature of the game, there are a variety of ways of placing an object in the scene. As @KevLoughrey mentions, you can use Camera.ScreenToWorldPoint(). If we assume the rotation of the camera is (0,0,0) with your object in front of the camera, then the 'z' position will be at some specific distance in front of the camera. So your code might look like:
void Update ()
{
if(Input.GetMouseButtonDown(1))
{
float dist = transform.position.z - Camera.main.transform.position.z;
Vector3 pos = Input.mousePosition;
pos.z = dist;
pos = Camera.main.ScreenToWorldPoint(pos);
pos.y = transform.position.y;
transform.position = pos;
}
}
So we first calculate the distance between our object and the camera on the 'z' axis. Then we create a Vector3 composed of the mousePosition and the distance as the 'z' component. ScreenToWorldPoint() converts that Vector3 into a world position. Since you did not want the 'y' component, we overwrite the 'y' component with the current one of the object, and finally assign the result to 'transform.position'.
Answer by KevLoughrey · Apr 22, 2014 at 04:40 PM
Input.mousePosition
Yes, but how do you make it only return the X and Z coordinates, and how do I assign those values only to the instance I'm moving?
Input.mousePosition returns a vector3, so there's no way to only return the x and z values. You had the right idea above, and should use something like:
transform.position = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0)
If that's not working for what you need, then you might need to convert the mouse's screen position into gameworld coodinates, using Camera.main.ScreenToWorldPoint(Input.mousePosition)
, but you will need to modify the z value of mousePosition first or it won't work. If you do a quick google search, you can find a bunch of threads here about that.
There are two problems with you code.
As $$anonymous$$evLoughrey has said, Input.mousePosition
is what you need to use ins$$anonymous$$d of just mousePosition
. If that doesn't work, then ScreenToWorldPoint is a good idea.
The other issue is that you use -.45
as the third value. This will throw an error. In C#, there are different kinds of number. The main ones you will use are int
and float
. The difference is that int
supports only integer values, and float
supports decimal values. However, there is another type, double
, which supports decimal values to greater precision than float
. The two types are not the same, so C# requires that you deter$$anonymous$$e which you are using at any given time. By default, decimal values are converted to the double
type, but Unity's Vector3
class uses float
. In order to specify that a value is a float
and not a double
, postfix it with an f.
So, your -.45
should be -.45f
.