However, I don’t want the same semi-boring button gameplay, and wanted base building to be sort of like Lumber Tycoon 2 or Oaklands, where you buy a boxed structure, carry it over to your base, and then open the box to start placing the object.
But I’m having a bit of trouble with the “carry the box over to your base” part because I don’t know how they do the math to get the position of where the box should end up when you’re dragging it relative to your cursor.
Here is a video of what I’m talking about:
I just need an explanation of how they might get that CFrame/Position based off the Character and Mouse positions
What I believe they do is calculate the delta between the mouse position in world space and character position, with that, clamp it to a maximum amount of studs so it stays near the character as seen in the video, and to keep it from going through parts, shapecast (or raycast) to check for collisions and reposition the object based on distance between origin and hit.
Above is the main thing, to make the movement smooth you can interpolate between the previous frame position and the current updated position with lerp functions (math.lerp)
How it is usually done I think is, a ray is casted from the camera to where the mouse is pointing to, and then another ray goes from the character to the end of the first ray. The length of the first ray is set at a limit, not sure how it is determined
But that is imo a flawed approach, because grabbing in third person is basically really not practical, it doesn’t feel good, and doesn’t work well. This is because this approach uses the character as “where the player is”, but the player doesn’t interact with the world from the character, they interact with the world from the camera
My approach almost completely ignores the character. It simply casts a ray from the camera to where the mouse is pointing in the world, and places the part at a set distance along this ray. The distance is initially set as the distance from the camera to the point being grabbed, but there is the issue of zooming in and out moving the part away from the character. This can be fixed by calculating the distance between the head and the camera, and when it changes, change the distance of the grab point along the ray accordingly
This is the result,
I might be a little proud of my grab system :P
I have not seen this approach used in any other grab system
what i do for the grab system i just created, is i have an AlignPosition constraint and set the position alignment something along the likes of
char.Head.Position + (FindPartOnRay.Position - char.Head.Position).unit * maxDistance
(this isnt exact, theres a bit more involved with the FindPartOnRay step, but yeah)
as far as i know, this is very similar to how Lumber Tycoon 2 does it, and works very well both in first and third person
Yeah I’ve used both AlignPosition and AlignOrientation for mine,
The way you are calculating it is I think like Lumber Tycoon 2, it’s based of the character. I wouldn’t qualify the Lumber Tycoon 2 grab as working well in third person, it’s hard to move things around in third person. It works, but your range of motion is very limited. Can’t throw stuff around either
You can join this game (a game I used to work for) to try out my grab system for yourself, and see the difference. This game doesn’t drop parts by default though, they float in the air when you release the mouse, you have to press e to drop them
I wish grab systems in games worked well in third person. I want to make my grab system open source in Community Resources, I haven’t done so mainly because it doesn’t work as well on mobile, and has no console support
It works, but your range of motion is very limited. Can’t throw stuff around either
yes, that is correct.
that is also how it is intended to work. imagine picking up an entire tree and then just throwing it around and moving it anywhere you want. Defaultio’s intent with his grabbing system seems to have been to keep a little realism, even if picking up an entire tree isn’t exactly “realistic” (there is a weight mechanic where you cant pick up trees that are too big, but you can still pick up most trees regardless)
that is also the kind of dragging system I want, something that’s more on the side of realism while still keeping it usable for more than just a few light boxes.
your dragging system is really cool though! it seems very fun and I would definitely play that game you showcased if it releases.