Hello. I have been researching a lot in regards to dragging objects like in many games (ie, skyrim, amnesia, etc) but I have yet to find anything satisfactory for 3D games. I am trying to make a lever that can be dragged up or down to toggle things but atm I am having a difficult time with the dragging part.
I’ve tried spring joints which came close but to the it being… a spring, the player has to look up into the sky to get the spring anywhere near the upwards rotation limit.
Most answers I find tend to be for 2D stuff and no explanation so it becomes hard for me to parse the code and translate it to unity3D.
Would anyone have an idea for how to allow me to drag an object/push a lever without animations OR make the spring component act more like a bar that connects the “hand” empty and object (detected via raycast.)
The answer is highly depended on what you actually want the interaction to feel like.
What you could try is create a component on your lever that implements a Rotate function, which rotates the lever on a plane between a minimum and maximum angle given some input lerp factor (a value between 0 and 1).
When you then look at the lever and for instance hold your mouse, you could raycast towards the lever, get that initial hit position (dragStartPosition), cache it and every frame compare the new raycast hit position to the original one.
You could then determine that lerp-value between 0 and 1 for your rotation function based on the distance vector between your cached dragStartPosition and the current hit position. This is the tricky part, you would probably want to do this by omitting the axis from the distance vector that are not on the rotational plane of the lever.
That way when you stand right infront of the lever (i.e. the lever should be pulled/pushed vertically) and you drag your mouse horizontally, the lever won’t move.
Another challenge would be that when you “grab” the top of the lever, your drag should be longer to move the lever to the other side, while when grabbing low on the lever, you could flip the lever with a shorter motion. Maybe include the distance from the grab point to the base of the lever into calculating that lerp value for the rotation.
Also, you could add dampeing when moving the lever to give it a bit more of a heavy feeling.
And yes, springs are notoriously combersome to work with. If you want some reliable, custom and tweakable lever, just do it all with code - as you said, you dont want springiness here anyways.