How do i make a projectile that has a range around the player

Hello I am trying to make a projectile that tweens to the mouse but doesn’t tween past a certain distance

example
my mouse is 50 studs away from the player but after that the projectile stops at 50 studs but if my mouse is below 50 studs away from the player the projectile tweens to the mouse.

I have tried using the difference between the player’s humanoid root part’s cframe x ,y and z coordinates and the mouse x y and z then checking if they are above or below, but this didn’t fully fix the issue because you needed to be facing a certain direction for it to be accurate. I have a idea of how this can be done but that is for time and does not stop after a certain distance or stop at the mouse.

This is my first time posting on the dev forum
Any kind of help is welcome

2 Likes

Ok so . To fire a projectile, you need 2 kinds of data . You need the ORIGIN and DIRECTION .

Origin is basically the start position of the projectile

Direction is the direction of the projectile you want to fire at

so, to get the origin , it is usually the handle of the projectile launcher or the head of the character

to get the direction you want the projectile to fire , it is a bit harder. you need to take the destination(usually the mouse position) subtracted by the origin. after that, you got to make the direction to magnitude of 1 . it will look something like this :

local Origin = Handle.Position
local Destination = mouse.Hit.Position

local Direction = (Destination-Origin).Unit

now, you need to know how to use raycast . raycast is basically like firing a laser from a position in a certain direction
eg)

workspace:Raycasy(Origin,Direction,Params)

Params is basically filter which dictates what object can or cannot block the laser . To set a raycast params, you simply write this

local Params = RaycastParams.new()
Params.FilterDescendantsInstances = {character} 

the reason you want to ignore the character is so that you cannot shoot yourself

now, you got to fire the projectile. you can do it by

workspace:Raycast(Origin,Direction*50,Params)

the reason you want direction*50 is so that the projectile will only travel 50 studs

1 Like

this also works if it hits something before it reaches 50 studs right?
well anyway thanks for the help

1 Like

Yep It does . You can change 50 to a higher number so that it have higher range :DD

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.