I want to implement a concept in unity application where user should drag object following checkpoints in a give path. Sprite/Object should not go far away from the path. I have implemented Move GameObject along a given path this concept. this is working fine when we auto move object with update function but when drags it is going any point. 1.) drag object in a sequence of checkpoints in a given path 2) restrict/fix sprite to give path while dragging
There are assets that let you set up paths and functionality like this easily. I use the [free] “Bezier Path Creator” by Sebastian Lague: https://assetstore.unity.com/packages/tools/utilities/b-zier-path-creator-136082
With the above, you can do the dragging restriction by using the VertexPath of a PathCreator to get the closest point along the path to a raycast hit point from the mouse position.
Vector3 worldPosition = hit.point;
Vector3 nearestWorldPositionOnPath = path.GetPointAtDistance(path.GetClosestDistanceAlongPath(worldPosition));
draggableObject.transform.position = nearestWorldPositionOnPath;
Thank you so much for your suggestion. It’s working now and saved my lot of time. But I have another challenge, I don’t want to go back on the path just want to use forward movement on the path.
You can set the EndOfPathInstruction to Loop, Reverse or Stop - .GetPointAtDistance(distance, EndOfPathInstruction.Stop);
, is that what you mean? Using Stop, any distance beyond .length will return the end point. With Loop it will start over from the beginning.
I want to drag only in forward direction. while dragging user can leave in-between number of time. I have successfully implemented the concept to start dragging from where it was left. but I am unable to restrict backward dragging over path.
From the following link, I found we can calculate distance traveled by object over path. As per my idea, we can check if distance travelled is greater than current distance then we drag success otherwise no dragging.
https://answers.unity.com/questions/1702609/how-can-you-make-a-path-with-a-switch-in-it.html
//Total distance traveled
distanceTravelled += speed * Time.deltaTime;
//Distance on this particular rail
railDist += speed * Time.deltaTime;
transform.position = pathCreator.path.GetPointAtDistance(railDist, endOfPathInstruction);
transform.rotation = pathCreator.path.GetRotationAtDistance(railDist, endOfPathInstruction);
But there is an issue of speed variable, In mouse drag, I didn’t used any speed variable so how can i implement this or any other script to handle dragging only in forward direction.
Do you mean preventing distance from decreasing when there’s a negative speed value?
float distanceDelta = Mathf.Max(0, speed) * Time.deltaTime;
distanceTravelled += distanceDelta;
railDist += distanceDelta;
Oh - without a speed value (sorry). You could just have …
private float distanceMin = 0;
Then you could say…
float newDistance = ..// get distance at hit point
if (newDistance < distanceMin) newDistance = distanceMin;
distanceMin = newDistance;
Edit: fixed mistake with last line
Can you tell in detail where we have to put the script to drag the object?
There is an official User Guide link on the asset store page - if that’s not enough to get started you could try contacting the publisher for support
This is a gem solution.
For my part, I used that code segment to have an object follow a path that is attached to its script (“Grabbable”) according to the mouse’s position. Here is the code :
Vector3 position = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.WorldToScreenPoint(selectedObject.transform.position).z);
Vector3 worldPosition = Camera.main.ScreenToWorldPoint(position);
Vector3 nearestWorldPositionOnPath = selectedObject.GetComponent<Grabbable>().GetPath().path.GetPointAtDistance(selectedObject.GetComponent<Grabbable>().GetPath().path.GetClosestDistanceAlongPath(worldPosition));
selectedObject.transform.position = nearestWorldPositionOnPath;