- Home /
Question by
shahanbutt0 · Dec 02, 2018 at 05:57 PM ·
unity 5mousemovediagonal
Drag object with mouse in diagonal direction
I have a gameobject which i want to move using mouse. Currently i can do that in x-axis and y-axis:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveObject : MonoBehaviour
{
public bool canMoveObject = true;
public LineController currentLineController;
public bool childGO;
public GameObject goToMove;
public bool moveDiagonal;
public bool moveX, moveY;
public float distanceXmin, distanceXmax, distanceYmin, distanceYmax;
public Vector3 screenPoint;
public Vector3 offset;
public Vector3 curPosition;
private void OnEnable()
{
if (canMoveObject)
gameObject.tag = "MoveableObstacle";
}
void OnMouseDown()
{
if (canMoveObject )//&& !PinchZoom.Instance.IS_ZOOMING)
{
currentLineController.CAN_DRAW = false;
if (childGO)
goToMove = gameObject.transform.parent.gameObject;
else
goToMove = gameObject;
screenPoint = Camera.main.WorldToScreenPoint(goToMove.transform.position);
offset = goToMove.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
//}
}
}
private void OnMouseDrag()
{
if (canMoveObject )//&& !PinchZoom.Instance.IS_ZOOMING)
{
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
if (moveDiagonal)
{
}
else
{
if (moveX)
{
if (curPosition.x >= distanceXmin && curPosition.x <= distanceXmax)
goToMove.transform.position = new Vector3(curPosition.x, goToMove.transform.position.y, 0);
}
else if (moveY)
{
if (curPosition.y >= distanceYmin && curPosition.y <= distanceYmax)
goToMove.transform.position = new Vector3(goToMove.transform.position.x, curPosition.y, 0);
}
}
//}
}
}
private void OnMouseUp()
{
currentLineController.CAN_DRAW = true;
}
}
I wanted to move my object diagonally as well how to do that according to its direction using mouse?
128459-capture.png
(43.0 kB)
Comment