- Home /
Drag object on XZ with perspective
Hi all! A bit of a noob here in Unity, I couldn't find quite what I'm looking for this.
I have a plane (viewed at an angle) and an object that falls gradually above it, like a 3D tetris.
I want to touch anywhere on the screen and drag the object on its XZ axis (the falling on Y axis happens automatically).
Left-Right movement works ok, but the Forward-Backwards movement (towards and away from the camera) is too slow (eg. moving my finger 1cm horizontally moves the object 1cm left-right, but moving it vertically the object only moves 0.2cm forwards or backwards)
How can I make it so that the movement is uniform in any direction?
For now this is what I have in my LateUpdate (or should that be in Update? ):
private bool dragging = false;
float posX;
float posZ;
float posY;
void LateUpdate()
{
if (Input.touchCount > 1)
{
return;
}
if (Input.GetMouseButtonDown(0))
{
Vector3 dist = Camera.main.WorldToScreenPoint(transform.position);
posX = Input.mousePosition.x - dist.x;
posY = Input.mousePosition.y - dist.y;
posZ = Input.mousePosition.z - dist.z;
dragging = true;
return;
}
else if (Input.GetMouseButtonUp(0))
{
dragging = false;
}
if (dragging)
{
float disX = Input.mousePosition.x - posX;
float disY = Input.mousePosition.y - posY;
float disZ = Input.mousePosition.z - posZ;
Vector3 newPosVector = new Vector3(disX, disY, disZ);
Vector3 lastPos = Camera.main.ScreenToWorldPoint(newPosVector);
transform.position = new Vector3(lastPos.x, transform.position.y, lastPos.z);
}
}
Thank you!
Your answer
Follow this Question
Related Questions
drag to move object in perspective camera 0 Answers
How to Restrict Movement of a Dragged Object to a Game Board? 1 Answer
Dragabble Logic for Card Matching game 0 Answers
Multitouch & IDragHandler 0 Answers
dragging an object with 1 unit 2 Answers