- Home /
how can do click then move on a gameobject?
Hello everyone.
This is what i am trying to do : click the gameobject, and after clicking, a directional arrow will appear (up, down, left, or right). The object has to be moved or dragged in that direction with the mouse. eg. directional arrow is right. The player will click the gamebject and move the mouse in the direction (right). This will indicate a right swipe or drag. The gameobject will be moved along the X axis by +1, but if the swipe/ drag is in any other direction but right, the gameobject will not move.
Here is the code that I am trying:
var startpos : Vector3 = Vector3.zero;
function Update () {
if (Input.GetMouseButton(0) && !touched)
{
var hitm : RaycastHit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if (hitm.collider != null && hitm.collider.gameObject == circle)
{
var mouseDelta : Vector3 = Input.mousePosition - startpos;
if(mouseDelta.x < -0.5){
movedir = left;
}
if(mouseDelta.x > 0.5){
movedir = right;
}
if(mouseDelta.y > 0.5){
movedir = up;
}
if(mouseDelta.y < -0.5){
movedir = down;
}
startpos = Input.mousePosition;
}
}
}
This isn't giving the desired result. The gameobject is being moved with the transform script :
if ( movedir == down){
transform.position = Vector3.MoveTowards(transform.position,Vector3(transform.position.x,
transform.position.y-1, transform.position.z ), 2* Time.deltaTime);
}
else if ( movedir == up){
transform.position = Vector3.MoveTowards(transform.position,Vector3(transform.position.x,
transform.position.y+1, transform.position.z ), 2* Time.deltaTime);
}
else if ( movedir == left){
transform.position = Vector3.MoveTowards(transform.position,Vector3(transform.position.x-1,
transform.position.y, transform.position.z ), 2* Time.deltaTime);
}
else if ( movedir == right){
transform.position = Vector3.MoveTowards(transform.position,Vector3(transform.position.x+1,
transform.position.y, transform.position.z ), 2* Time.deltaTime);
}
Can anyone help with this?
Answer by Jeff-Kesselman · Dec 22, 2014 at 06:15 AM
Is this the sort of thing you are looking for?
http://answers.unity3d.com/questions/12685/how-to-create-handles-like-editor-gizmos-in-runtim.html http://wiki.unity3d.com/index.php?title=Lipis_Runtime_Gizmo http://forum.unity3d.com/threads/rtgizmo-run-time-position-rotation-scale-gizmo.120007/
Hi, thanks for the response. Unfortunately, this is not what i'm looking for. I forgot to mention that this is 2D, and the gameobjects will be in a grid system (think 2048 in terms of the set up). I want that when the click occurs on one or any of the objects in the grid they can then swipe that gameobject into the direction of the arrow which is shown, be it right, left, up or down. I hope this helps to clarify what i mean.