Troubled with 2D to 3D directional velocities


Hello! Recently I’ve been struggling trying to figure out how I could make a swipe system: basically a system where you swipe with your mouse in a direction in 2D space, and then I take that and translate the direction into 3D space and then set velocity to a ball in said direction. This turned out to be harder than anticipated, I’ve tried numerous methods but still haven’t much luck. I do have some code here below, if anyone would have the time to try to see what I could do differently. One other note, the code below does work. However, the ball doesn’t always go in the right direction, which I’m trying to perfect somehow. Thanks for reading!

local Camera = game.Workspace.CurrentCamera

local Start = game.Workspace:WaitForChild("Cam_Part")

local End = game.Workspace:WaitForChild("End")

local mouse = game.Players.LocalPlayer:GetMouse()

local uis = game:GetService("UserInputService")

local db

local mdown = false

local ball = workspace.ball

Camera.CameraType = Enum.CameraType.Scriptable

Camera.CFrame = Start.CFrame

function begin_swipe()

local start_ =,mouse.Y)

while mdown do wait() end

local dist = (,mouse.Y) - start_).magnitude / 5

if dist > 40 then

local ray_ = mouse.UnitRay.Direction

local diff = (ray_ - ball.Position).unit

ball.Velocity = diff * 25


db = false



I have a few suggestions for your coding style:

  1. Please indent! A single tab makes a huge difference.
  2. Don’t use every other line. Use line spacing to differentiate parts of code that are doing different things.
  3. Don’t use underscores unless dealing with metatables. Instead use descriptive names like mouseRay.

Now, to solve your problem, you should take the camera position into account when determining which direction in the world the swipe is. My geometry is pretty poor but I’m sure somebody here can tell you how to translate movement on the 2D plane that is your camera screen into 3d coordinates of the world. I think a user is doing something similar here: Converting Image Coordinates to World Coordinates


Camera.CFrame.UpVector and Camera.CFrame.RightVector should be the desired directions. To convert a swipe to the corner of the screen we’d do something like this:

local function swipe(xDist, yDist)
    return xDist * Camera.CFrame.RightVector + yDist * Camera.CFrame.UpVector

the xDist and yDist is the distance traveled in x and y pixels from the point the user began touching the screen. You may wan to wait a bit to see how strong of a flick they do, apply this over time while they swipe, or find the unit vector of this direction and scale it by a constant magnitude (this direction may not be too accurate because pixel movements are often registered quickly at around 1-3 pixels at a time).


Interesting stuff. Thanks guys, one thing to note as well is this system is a swipe system for computer. It’s a bit unconventional, but I’m trying to create a different kind of mechanic for my game. I’ll definitely attempt to implement these things to my code, but there’s no guarantee I’ll be able to comprehend or figure out what exactly needs to be done. Any further explanations or info on this subject would be greatly appreciated as well.

Thanks guys!


Well, I’m still a bit confused by all of this, and I’ve been unable to come up with any working methods. Any further help on this would be appreciated, the code below is what I just tried using, but it really seemed to rely more on the camera’s facing position rather than where the mouse is pointing at. Not 100% sure how this function even works.




Alright well, I did get it to work. Not sure if it’s gonna be a good game mechanic or not, but I did get it to work. (finally!)

Thanks to all who replied :slight_smile: