Hey guys, so I’ve been trying to think of a new way I could change the functionality of my bowling game, to possibly utilize a swiping mechanic of your mouse.
Basically, how this would work is, if you swipe your mouse fast enough, in a large enough swipe, that will act as the ball’s direction.
I tried it one way using magnitude and getting the mouse’ position in the game world, but this yielded some really unpredictable results, as if the mouse was pointing at the sky or something, the magnitude would appear as some huge number.
I thought of maybe using a GUI frame element and then doing some manipulation with that. However, that would end up with a Vector2, and there’s still the matter of translating that direction into the game world.
You could try to use Mouse.X and Mouse.Y, instead of 3D magnitudes.
Velocity = Distance / Time
On click start, save the Mouse.X and Mouse.Y as a variable, as well as the time it happened
local swipe_start = {
Time = tick();
Position = Vector2.new(Mouse.X, Mouse.Y);
}
Do the same for when the click ended (Button1Up, InputEnded, etc)
local swipe_end = {
Time = tick();
Position = Vector2.new(Mouse.X, Mouse.Y);
}
Calculate…
local swipe_velocity = (swipe_start.Position-swipe_end.Position).Magnitude/(swipe_end.Time-swipe_start.Time)
Now you have the velocity of the swipe, for which you can do what you please with!
You can expand this to get the direction of the swipe by doing
local direction = (swipe_end.Position-swipe_start.Position).Unit
As for translating into 3D space, you could use the direction, with a predefined Vector2 (Maybe the middle point between swipe_start and swipe_end), and use Camera:ScreenPointToRay to calculate directions in 3D space for whatever you need it for.