the downloadable version looks way nicer than the web version btw ^^

this is a recoil system for first person shooter type games.

feel free to fork it on github

If you’re curios about how it works but don’t want to read the code. It’s pretty simple.

I’m using two main variables: target_recoil and recoil. both of witch are 3d vectors.

whenever the gun is fired, I add some value to target_recoil.
target_recoil += Vector3(1, 2, 3)

then every frame I move target_recoil towards zero.
target_recoil -= abs(target_recoil) * delta

as well as interpolating recoil towards target_recoil.
recoil = lerp(recoil, target_recoil, delta)

then I update the rotation of the camera.
camera_rotation = look_rotation + recoil

StatusPrototype
CategoryOther
PlatformsLinux, HTML5
Rating
Rated 5.0 out of 5 stars
(2 total ratings)
Authoratornity
Made withGodot
TagsFPS, Godot, Open Source, recoil, tech-demo

Download

Download
godot-recoil-system-win.zip 25 MB
Version 3 Apr 20, 2023
Download
godot-recoil-system-linux.zip 25 MB
Version 5 Apr 20, 2023

Comments

Log in with itch.io to leave a comment.

(+1)

This is awesome! I was trying to figure and research some ways to implement a recoil mechanic in my demo and I was so lucky I stumbled upon this. Thanks for sharing!

happy to be of service! 🫡

(+1)

Hello @Atornity!

I recently completed my first published Godot project / demo which had implemented and modified the original recoil system you had published. I would like to thank you again for sharing your insight and project. Without it I most likely would've been still using my original stiff and rigid "push-camera-up" recoil which wasn't ideal haha.

I made sure to give you a shoutout in my project's credit page and have a video published showcasing it. Cheers!

(+1)

Looks awesome! Thanks for sharing :3

(+1)

Really awesome!! I was trying to add some basic movement to the project but not sure how to determine what's forward since your project rotates the camera only and wouldn't rotate the root player node. Any solution?

(+1)

figured it out! replaced camera_rotation = look_rotation + recoil

with these three lines

var final_rotation = look_rotation + recoil

camera_rotation.rotation.x = final_rotation.x

player.rotation.y = final_rotation.y

(+1)

glad you got it working!

(+1)

also, the code you refered to "camera_rotation = look_rotation + recoil" is not the real code.

the actual code can be found here:

https://github.com/atornity/godot_recoil_system/blob/master/scripts/camera.gd#L7...

just in case you didn't know :3

(2 edits) (+1)

glad you liked it! I see you figured it out already witch is great. The way I would do it is a bit different.

for move left/right I would use "camera.basis.x" as the move direction. for forward/backward I would use "player.basis.y.cross(camera.basis.x)".

which might look something like:


var input: Vector2 = Input.get_vector(...) # replace '...' with move input mappings

var forward_axis: Vector3 = player.basis.y.cross(camera.basis.x)

var right_axis: Vector3 = camera.basis.x

var move_vector = input.x * forward_axis + input.y * right_axis

player.move(move_vector * speed)


I personally prefer to keep the camera rotation completely separate from the player rotation but that's completely up to you of course :3

(1 edit) (+1)

I see that's very interesting. are there any benefits to keeping the rotation of the player and the camera separate?

(1 edit)

1. it allows your camera script to exist without having a reference to a player. Which might be useful sometimes.

2. camera node does not have to be a child of the player node (although it can still be if you want)

3. for first person games, it allows you to attach a legs/feet mesh to your player without it being dependent on the rotation of the camera.

4. for third person games, it allows for more smooth camera movement where the camera isn't actually attached to the player (2). It also allows the player to have a different rotation from the camera which is very common for third person games.

5. it allows you to use the same camera script for first person games and third person games. (and other types of games probably)

It's all kind of small benefits that can be easily worked around. In the end it comes down to personal preference more than anything :3