Making projectile face mouse

Hello, I’m trying to make a projectile face at my mouse, but keeping it’s orientation so it shoots towards the mouse, but doesn’t face towards it.

  1. What do you want to achieve?
    I’m trying to shoot my projectile towards the mouse, while keeping it’s orientation.

  2. What is the issue?
    The issue is, that if I use Mouse.Hit, it will make them have the orientation of where the mouse is at, and if I use workspace.CurrentCamera:ScreenPointToRay(), the projectiles do what I want them to do, but they only do so in first person, if I move the camera down in third person and aim my mouse at a target, the projectile will not go to the target, but to where the camera is pointing at.

  3. What solutions have you tried so far?
    I’ve tried Mouse.Hit, but it makes the projectiles gain the orientation of where the mouse is at. I’ve tried workspace.CurrentCamera:ScreenPointToRay() and it works, but only in first person, if I go in third person then the projectiles are offset.

Code sample of the ScreenPointToRay() method.

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local Mouse = LocalPlayer:GetMouse()

local Character = LocalPlayer.Character
local HumanoidRP = Character:WaitForChild("HumanoidRootPart")

local ray = workspace.CurrentCamera:ScreenPointToRay(Mouse.X, Mouse.Y, 1)

local Origin = (HumanoidRP.CFrame * CFrame.new(0, 0, -2)).Position
local Direction = CFrame.new(ray.Origin, ray.Origin + ray.Direction).LookVector

local Part = Instance.new("Part")
Part.Size = Vector3.new(2, 2, 2)
Part.Anchored = true
Part.CanCollide = false
Part.CFrame = CFrame.LookAt(Origin, Direction)
Part.Parent = workspace

Result:

Part faces the angle of the camera instead of the mouse’s position.

Thanks in advance,

Jerememez.

1 Like


The reason why I don’t want to use Mouse.Hit is because, if I fire 3 projectiles with a gap of 0.5 studs between each other, they will only overlap at the Mouse.Hit.Position, but with ScreenPointToRay() they overlap at a certain distance from where they were cast from, which is what I want, but the problem is that ScreenPointToRay() makes them face the camera.

2 Likes

What does this even mean? I don’t understand what you’re trying to achieve.

how i understand it is that u want to shoot the projectile towards the mouse but u do t want it to change orientation? in that case try using Mouse.Hit.p as it gets the position of the mouse instead of its CFrame

I tried to explain it the best I could, but basically I’m trying to make it so the projectiles get fired TOWARDS the mouse position and not AT the mouse position, because firing multiple projectiles at Mouse.Hit (CFrames the projectiles AT the mouse position) makes the projectiles intercept at Mouse.Hit instead of at a certain position away from the origin point. I’m really bad at explaining things, but I’m trying my best.

Try using the cframe.lookat function

Mouse.Hit is the position where they intersect. If you want them to intersect somewhere else you have to define a different position for them to head to.

It wont let me edit but try making the part lookat the mose by doing mouse.hit.p

The thing is, I want them to intersect while heading towards the mouse, not when they arrive there. That’s the point of why I made the post, which is making them head towards the mouse and intersect at a certain point, that isn’t Mouse.Hit, ScreenPointToRay() worked to some extent, but as I stated in the post, they only do so in first person, if I zoom out, and point my camera down, and point my mouse somewhere else, the knives will head towards the camera, instead of the mouse.

You need to define an intersecting point if you want them to intersect somewhere. If you want them to just go in the direction of the mouse then they’ll all go the same direction and be parallel and never intersect.
image

I think just making them go in the direction of the mouse without intersecting is better, here’s a video of what I meant in the last reply.

The knives get offsetted if I zoom out, but they achieve what I want them to do if in first person.

Perhaps try using this?
https://developer.roblox.com/en-us/api-reference/property/Mouse/UnitRay
Use the direction of the unitray as the direction for the knives.

Still seems to cause the same issue, it depends on the camera angle and not the location of the mouse.

According to the API it’s based off of the Camera’s WorldPosition

I was thinking you’d make the player’s humanoid root part face towards that unitray direction.