- Home /
using mouse to move an object
I have created an object in my scene, what exactly I wanna do is that, I want to click on my object using my left click (mouse) and the object moves towards a certain point, then clicking back again on the same object to return to it's position. which method can I use to do this function and if possible can somebody include an example of a script or something, I am totally new to this unity game engine.
Can you be a little more specific about what you are wanting? Are you wanting to click a point in 3D space and move the object or are you wanting to click the object itself and then move it?
Are you saying click and drag the object and have it move? If so, I just figured out how to do this and I'll be glad to help you out.
Answer by GambinoInd · Oct 05, 2013 at 10:41 PM
Add a collider to the object you wish to move. You should also put it on a layer that holds all movable objects.
Next you want to create a script that sends a raycast from the camera using something like this?
int layerMask = (1 << LAYER_FOR_MOVABLE_OBJECTS);
RaycastHit hit = new RaycastHit ();
if(Physics.Raycast (Camera.main.ScreenPointToRay (Input.mousePosition), out hit, 1000, layerMask)) {
//Get hit.point.x, and hit.point.y, and hit.point.z to grab the position of where it hit
}
If you are making the game 2D or from a side view, you could dont have to raycast and you could just get the Input.MousePosition and move the object on 2 axis (like x and y) based on the mouse movement. Moving it around in the 3d world requires the code i just posted above, but you're going to have to figure the rest out.
Answer by Cherno · Oct 05, 2013 at 11:28 PM
Your box needs a Box Collider and the following script:
var ObjectLayerMask : LayerMask;//Select the layer your box or whatever is in
var Position1 : Vector3;//leave empty, it gets filled automatically in Start()
var Position2 : Vector3;//enter the coordinates of your desired location to move towards
var MoveSpeed : float = 4;
var CurrentPos : int = 1;
function Update()
{
var ray = Camera.main.camera.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if(Input.GetButtonDown("Fire1"))
{
if (Physics.Raycast (ray, hit, Mathf.Infinity, ObjectLayerMask))
{
Move();
}
}
}
function Move()
{
var StartPosition : Vector3 = transform.position;
var EndPosition : Vector3;
if(CurrentPos == 1)
{
EndPosition = Position2;
CurrentPos = 2;
}
else
{
if(CurrentPos == 2)
{
EndPosition = Position1;
CurrentPos = 1;
}
}
var t : float = 0.0;
while (t < 1.0)
{
t += Time.deltaTime * MoveSpeed;
transform.position = Vector3.Lerp(StartPosition, EndPosition, t);
yield;
}
}
Your answer
Follow this Question
Related Questions
Moving an object to the location of a mouse click... 2 Answers
Button doesn't work when returning to scene (android) 1 Answer
Want sprite to move to mouse click position instead of teleporting or moving a few pixels 1 Answer
Extending Unity's UI components 1 Answer
I want OnClick to fire the same frame that the button is clicked 1 Answer