Help with dragging an object along an axis

Can someone help me out with this? I’m just not getting it.

    Vector3 startDrag;
    Vector3 currentDrag;
    bool isDragging = false;

    void Update()
    {
            if (Input.GetMouseButtonDown(0)) { isDragging = true; startDrag = _.worker.view.ScreenToWorldPoint(Input.mousePosition); }
            if (Input.GetMouseButtonUp(0)) { isDragging = false; }

            if (isDragging) {
                currentDrag = _.worker.view.ScreenToWorldPoint(Input.mousePosition);
                float deltaX = currentDrag.x - startDrag.x;
                Debug.Log(startDrag.x + " " + startDrag.y + " " + startDrag.z);
                target.position = new Vector3(target.position.x + deltaX, target.position.y, target.position.z);
            }
    }

Mainly I don’t understand how ScreenToWorldPoint works. It’s supposed to take a screen point and then convert it to a world position but how?

I debug my startDrag results but I don’t understand what its giving back to me.

It’ll give me this.

0 1.45 0

But then I move a hair to the right and click and then it gives me this.

-2.647362E-08 1.45 4.82518E-08

Which doesn’t make sense to me.

Those numbers are just scientific notation for very, very small numbers, so probably just due to floating point inaccuracy.

What is the actual issue here?

Thx for reply spiney. I’m basically trying to move an object along an axis in the exact same way you would do in the unity editor when clicking on the red/blue/green arrows and dragging your mouse.

Do you know of any tutorials? Everything I’ve seen uses onMouseDrag, etc.

I was asking what is not particularly working about your current implementation. I can work out what you’re trying to do from the thread title.

In any case you just need to ensure you’re dragging along a particular plane. I’ve attached an example scene/scripts.

MosueDragExample.unitypackage (4.2 KB)

Edit: Nice typo in the file name, me.

I just figured it out spiney. Why do I always figure it out right after posting?

I found a post that showed how to create a plane and I didn’t know you could do this. This is exactly what I’ve been wondering about.

First I create a plane.

    Plane plane;

And then set the plane when user clicks on a gameobject they want to drag.

            if (Input.GetMouseButtonDown(0)) { 
                isDragging = true; 
                plane = new Plane(transform.up, target.position);
            }

And then when I drag I shoot a ray from camera onto the plane and see where it intersects and just set the target position to whatever axis of the hit point.

            if (isDragging) {
                Ray ray = _.worker.view.ScreenPointToRay(Input.mousePosition);
                float hit = 0.0f;

                if (plane.Raycast(ray, out hit)) {
                    Vector3 point = ray.GetPoint(hit);
                    target.position = new Vector3(point.x, target.position.y, target.position.z);
                }
                target.position = new Vector3(target.position.x, target.position.y, target.position.z);
            }

Now that I’ve learned about planes I feel like I can do anything.