Drag object around in worldaxis x and y. Object moves in x and z instead

I cant figure out why my object still moves in x and z axis while dragging. I suspect that the transform uses the object oriantationaxis and not the world ones(the object is rotated). If this may be the problem how can i change the transform oriantation in my code?

private Vector3 screenPoint;
private Vector3 offset;
void Start()
{
}

void Update()
{

}

void OnMouseDown()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo, 1000f, 1 << 9))
{
screenPoint = hitInfo.point;
//currentPlaceableObject.transform.rotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
}
screenPoint = Camera.main.WorldToScreenPoint(transform.position);
offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}

void OnMouseDrag()
{
GameObject.Find(“CameraRig”).gameObject.GetComponent().enabled = false;
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
transform.position = curPosition;
}

private void OnMouseUp()
{
GameObject.Find(“CameraRig”).gameObject.GetComponent().enabled = true;
}

Please use the code formatting provided when posting code

You can use transform.TransformVector, transform.TransformPoint or transform.TransformDirection to change from local to world space

1 Like

I am sorry will do this next time.

And thx for your help i got it work.