Hi,
I have tried several ways of doing this but I cannot get it right. I'm basically sliding a cube on a plane. The plane is just there for visual effect. I don't want to use a rigid body, etc. I'm using the following code for the dragging:
void OnMouseDown()
{
screenPoint = Camera.main.WorldToScreenPoint(transform.position);
offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
void OnMouseDrag()
{
Vector3 dragPosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
dragPosition = Camera.main.ScreenToWorldPoint(dragPosition) + offset;
dragPosition.y = 2.0f;
transform.position = dragPosition;
}
The cube needs to slide around the screen but remain of a y value of 2.0f. The cube moves on the X and Z axis but I want the y-axis to stay constant on 2.0f, but it goes out of sync with the cursor. Can anyone give me a pointer in the right direction?
↧