How would I be able to calculate the way that my gun can shoot at the player?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to make an npc that shoots at a player.
  2. What is the issue? Include screenshots / videos if possible!
    The issue is I don’t know how to calculate the direction of which the raycast will be done at.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Not much.
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local RightVector = ShootPos.CFrame.RightVector
					local Direction2 = -RightVector * 1000
-- the reason why it's right vector is because thats the front of the gun. Lookvector gets the side.

This is the code that calculates the direction. But it doesn’t directly shoot at the direction of the player which is what I want. If you want the full code then simply ask below.

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

I tried the things in studio. They don’t even shoot at me. So I dont see how this helps me.

You need to configure them correctly, check the documentation page for more info.

I still dont get it, can you please explain it?

I’ve litterally tried everything I could’ve thought of -_-. Please help someone.

Somone please help. I LITTERALLY can’t think of anything to do this. Someone please help!

Does anyone have any ideas how I can do this. please. I need to know!

calm down, stop spamming, use magnitude it fixes the solution

Can you please show an example? I don’t know how this would work.

Just look up how to make a looking at player npc. LOL simple as that.

But the code will be too complex so I recommend you get in the advanced stage atleast to understand it

I know how to use magnitude. I’m just wondering how this will work.

you have to while true do on players humanoid root part and some complex calculating stuff, just search on YouTube how to make starring at player npc I’m not such a good teacher myself

https://www.google.com/url?sa=t&source=web&rct=j&url=https://m.youtube.com/watch%3Fv%3DRwhwbPiBMnc&ved=2ahUKEwjl06Ong_jzAhWPjKQKHbq8BQ4QwqsBegQIBBAF&usg=AOvVaw3l9UU7LyUHo1Pjfl3ETD-j

I know how to use the look at function of cframe. Plus the npc wont be still. So if they use look at they move which can mess up the move process.

I’m on mobile rn, so I can’t really do much, I might later get on PC and tell you how it works

In order to get the direction from NPC to the Player get the unit vector of the difference between their positions.

-- position of the target Player
local playerPosition = Player.Character.HumanoidRootPart.Position

local gunPosition = ShootPos.Position

-- Use this direction variable for the raycast function
local direction = (playerPosition - gunPosition).Unit

local rayLength = 500 -- the length of your bullet ray in studs

local raycastResult = workspace:Raycast(gunPosition, direction * rayLength)

I don’t know if I did anything wrong, but it doesn’t seem to work.
This is the new direction.

local Direction = (HRP.Position - ShootPos.Position).Unit

If you can please help that would be great. Thanks for the help anyways tho.

That direction vector should work. Are you creating the ray properly?