Moving on the XZ plane
Hi,
I have been around the internet, trying to figure out how to move an object on the XZ plane.
I have a script that almost works. Here it is:
using UnityEngine;
public class DragObject : MonoBehaviour
{ Vector3 dist; Vector3 startPos; float posX; float posZ; float posY;
void OnMouseDown()
{
startPos = transform.position;
dist = Camera.main.WorldToScreenPoint(transform.position);
posX = Input.mousePosition.x - dist.x;
posY = 0;
posZ = Input.mousePosition.z - dist.z;
}
void OnMouseDrag()
{
float disX = Input.mousePosition.x - posX;
float disY = 0;
float disZ = Input.mousePosition.z - posZ;
Vector3 lastPos = Camera.main.ScreenToWorldPoint(new Vector3(disX, disY, disZ));
transform.parent.position = new Vector3(lastPos.x, 0, lastPos.z);
}
}
My problem is when I attach it to my object, the object 'jumps' every time in the Y direction, if I use:
posY = startPos.y
If I use :
posY = 0
it still jumps up a little but then 'hops' towards the camera.
The script works perfectly for a regular object (eg a cube) but I am trying to use it for an imported object that doesn't have a box collider. So I have a cube, that isn't rendered, as a child object (hence the last line, which transforms the parent object (transform.parent.position).
Can anyone suggest a solution?
Thanks,
Connal
PS - Sorry about the formatting. Not quite sure why some of it is laid out 'nicely' and the rest is in a heap...