How to make a realistic smooth flashlight

Hey there, my name is ShieldDevs, you can call me shield. I’m a scripter and a game developer.

So lately I’ve been working on a scary game, “Escape the Unknown”, and I was trying to make a realistic flashlight that’s attached to the head. And I’ve seen a lot of horror/scary games, and the flashlight there is a bit slower, it follows the camera with a bit of slow, smooth movement, it’s in almost all games.

Here’s an example I found earlier today, WARNING VIDEO CONTAINS LOUD NOISES AND JUMPSCARES, WITH DARK SCENES. YOU HAVE BEEN WARNED. Terrifying upcoming horror game They Are Here and the weird history it's based on - YouTube

And if you go to 1:51, you’ll find the flashlight part, where the flashlight slowly follows the camera/player.

I’ve seen a lot of games on ROBLOX that have the same thing, but I don’t know how to make it.

I would really appreciate it if you can help me!

Thanks,
ShieldDevs

3 Likes

I think there are two possible ways to achieve this effect you’re looking for, the first one would be to use TweenService and the other is you use the CFrame:Lerp() function.

3 Likes

If I’d use one of them, how would it look like tho? I have no idea how to make it

1 Like

you can look at how the classic flashlight tool works, also making the players head point to their cursor achieves this affect, however the latter is hard to replicate to server smoothly without loosing performance and is mostly just on client.

3 Likes

In the link I provided you can check how to properly use the TweenService as it comes with examples too. CFrame:Lerp() is used within a loop, you give it a goal and then an alpha, the goal would be the CFrame which you would have to calculate, it would need to include the position of the flashlight and it’s rotation (pointing to the mouse), think of alpha as how “snappy” the movement would be, 0.3 works fine.

2 Likes

Yeah, I’ve been moving the head with the cursor, and it kind of does that, but when the player’s FPS is too high, it becomes as if it’s just a normal flashlight. And thank you for your answer, really appreciate it.

Ok I’ll try using Lerping, because I advanced TweenService and I can’t seem to find it useful in this situation, so I’ll look around for Lerping, hopefully it’ll help me reach there. Thank you!

TweenService may be more useful here as you can change it to how you’d want to look like, for example the EasingStyles, duration and other.

1 Like

TweenService is indeed useful, it basically allows to get a smooth transition between two things, in this case you can just use TweenService to smoothly interpolate the head’s CFrame.

1 Like

Infact, BodyMovers could be the way to go around doing this, if you’re going for the effect on the video, using BodyMovers to make the light part follow your character’s orientation and movement could work.

1 Like

Yeah but here, the light is a child of the head in this case, how would I tween it?

You would tween it like you would tween any other part as it doesn’t matter if its in the head. However, as mentioned above it might be better if you went for BodyMovers as you’d constantly be having to create tweens and play them whereas with BodyMovers, you’d get a more realistic effect (or atleast whatever i’m imagining in my head seems more realistic) and probably easier to implement.

1 Like

It’s the same thing with BodyMovers, you need to constantly be feeding them the new information for them to be accurate plus they don’t work well with welded parts. I’d rather just use TweenService or CFrame:Lerp() on the Head’s C0.

1 Like

Yes, you are correct however, I was suggesting that using BodyMovers may have the realistic effect he’s looking for.

1 Like

That can be achieved with TweenService or Lerp, with TweenService as long as the duration isn’t instant and for Lerp as long as Alpha isn’t 1 he’ll achieve that sort of smooth “delayed” motion.

1 Like

If you used lerps (as I personally would), it would look something like:

flashlight.CFrame = (CFrame.new(camera.CFrame.Position)*(flashlight.CFrame-flashlight.Position)*offset):Lerp(camera.CFrame*offset,0.5)

In a renderstepped event or something
You could edit the 0.5 to be lower or higher to get a faster or slower effect respectively
Offset is the cframe offset relative to the camera (so for example offset = CFrame.new(2,-1,3) would make the flashlight 2 studs to the right of the camera, 1 stud down, and 3 studs in front of the camera)

I wouldnt recommend tweenservice because the camera is constantly changing directions and unless your tween is a 60th of a second long it will be super choppy (and when the tween is a 60th of a second long the easing you can usually easily get with tweenservice practically doesnt exist) (its also kinda overkill to have a whole looped fancy movement with all kinds of easing and new threads and stuff taking up performance for an effect youll mostly not even see, especially when it can be done in a single line of code)

You could use bodymovers but in my personal opinion lerping is way easier and more naturally mixes with the non physics camera system

2 Likes

How would I use BodyMovers? Sorry, it’s my first time knowing about it.

Thank you for your reply. So I used the script and the information you used above, but how is there a CFrame for the flashlight?

I’ve done it using BodyMovers. Thank you all for your responses, and special thanks to @PapaBreadd, @skillednames, and @Slazerick!

2 Likes