how do u sample perlin noise to use for camera shakes? for example , adding camera recoil when firing weapon im using alpha6 and the noise functions seem to have changed

25 days later

im trying to add some camera shake im calling the camerarecoil from weaponscript i want to add subtle rotation on x and z to the $CamRoot based on perlin noise and go back to the original position

i know i can setup noise by

@onready var noise = FastNoiseLite.new()

and getting the value

(noise.get_noise_1d(value)
func _input(event):
	if event is InputEventMouseMotion:
		# Rotates the view vertically
		$CamRoot.rotate_x(deg2rad(event.relative.y * MOUSE_SENSITIVITY * -1))
		$CamRoot.rotation.x = clamp($CamRoot.rotation.x, deg2rad(-80), deg2rad(80))
		
		# Rotates the view horizontally
		self.rotate_y(deg2rad(event.relative.x * MOUSE_SENSITIVITY * -1))
func cameraRecoil():
	#perlin noise random rotation and back to original?
	$CamRoot.rotation.x = deg2rad(-2)
	$CamRoot.rotation.z = deg2rad(-2)

that didnt work in 3d so i made a very basic version

var randomnumber = random.randf_range(-1, 1)
	var offset2 = (noise.get_noise_1d(randomnumber)) 
	zkick = offset2
	$CamRoot.rotation.x += deg2rad(recoil)
	$CamRoot.rotation.z = deg2rad(offset2)
	#print($CamRoot.rotation.z)

Yeah the code there is for 2D, but watching the GDC video embedded there should give you some idea of 3D too, namely that you don't want to do positional/translation in 3D, only rotational.

my basic code works but isnt very good i know. for example, the rotation z doesnt get smoothly set to zero again

I really recommend watching the GDC video, the trauma value works really well.

ill watch it later been working on art assets so my current script will have to do for now

@Megalomaniak

im trying out the trauma function get noise 2d gives "nan" errors, this is probably cause noise functions have changed in godot4 also the camera isnt resetting to original rotation over time


@onready var noise = FastNoiseLite.new()
var noise_y = 0
var trauma = 0.0  # Current shake strength.
var trauma_power = 2 # Trauma exponent. Use [2, 3].
func _ready():
noise.noise_type = noise.TYPE_SIMPLEX_SMOOTH
	noise.frequency = 16.0
	randomize()
	noise.seed = randi()

func _process(delta):
	
	if trauma:
		trauma = max(trauma - decay * delta, 0)
		shake()
		
func add_trauma(amount):
	trauma = min(trauma + amount, 1.0)
	
func shake():
	var amount = pow(trauma, trauma_power)
	noise_y += 1
	$CamRoot.rotate_z(deg2rad(max_roll * amount * noise.get_noise_2d(noise.seed, noise_y))) #"math		is_nan'v.x" is true  > error
	$CamRoot.rotate_x(deg2rad(max_roll * amount * noise.get_noise_1d(noise_y)))
	print(noise.get_noise_1d(noise_y))#prints correct values

and i get weird result when looking around. probably cause im also moving the camera in input

func _input(event):
	if event is InputEventMouseMotion:
		# Rotates the view vertically
		$CamRoot.rotate_x(deg2rad(event.relative.y * MOUSE_SENSITIVITY * -1))
		$CamRoot.rotation.x = clamp($CamRoot.rotation.x, deg2rad(-80), deg2rad(80))
		
		# Rotates the view horizontally
		self.rotate_y(deg2rad(event.relative.x * MOUSE_SENSITIVITY * -1))

apparently this line didnt work and was always 0

noise.seed = randi() 
#just had to change it to seed 1

still getting weird behaviour when looking around and calling the shake function

the camera doesnt reset to original rotation of the start of the shake. it works sort of ok when im not looking around. the shake starts to mess up the "looking" rotation of the camera for example when having heavy trauma, the camera z rotation doesnt reset to zero value

i also cant figure out on how to setup perlin noise in godot 4. cant find docs on how to set it up noise type symplex smooth isnt really smooth ive noticed

@DJM said: the camera doesnt reset to original rotation of the start of the shake. it works sort of ok when im not looking around. the shake starts to mess up the "looking" rotation of the camera for example when having heavy trauma, the camera z rotation doesnt reset to zero value

Perhaps if you give the camera a parent node and make the mouse look/inputs control that, while the trauma shake affects the camera.

I can't really comment on godot 4 since I haven't really invested much time in that yet, but sounds like it's to do with the noise resolution(I don't mean frequency by that, but the resolution of the target it's rendered to, i.e. width and height properties).

ive allready tried that same result. it was worth the try ill switch back to my simple recoil script for now. the docs arent up to date so i cant figure out the noise functions.

7 months later