Weird jittering when loading into level after adding aiming

So using the script exactly like Bram the aiming does not work at all.
After modifying it a bit by swapping weapon_camera_fov for weapon_camera.fov made it work as intended when pressing the button but now there is an issue that when loading in to the sandbox the camera spazzes out and zooms in and out during the first few frames.

I tried moving and swapping some lines but it either completely breaks the aiming function or still causes the jittering zoom in and out on load issue.

func _process(delta: float) -> void:
	if Input.is_action_pressed("aim"):
		smooth_camera.fov = lerp(smooth_camera.fov, smooth_camera_fov * aim_multiplier, delta * 15)
		weapon_camera.fov = lerp(weapon_camera.fov, weapon_camera_fov * aim_multiplier, delta * 10)
	else:
		smooth_camera.fov =  lerp(smooth_camera_fov, smooth_camera.fov * aim_multiplier, delta * 25)
		weapon_camera.fov =  lerp(weapon_camera_fov, weapon_camera.fov * aim_multiplier, delta * 20)

I’ll see if I can shed some light on this.

First off, lerp took me a while to get used to, but the parameter list looks like this:

  • var_to_be_lerped = lerp(
  • var_to_be_lerped,
  • target_value_to_lerp_to,
  • percentage_of_the_way_to_go)
    And by calling this multiple times, you get a nice asymptotic curve.

Based on this, the arguments for your lerp() functions in the else-statement need to be swapped. As it stands now, those statements are actually lerping what is supposed to be your target.

The other part of the problem is that your if-lerps and your else-lerps aren’t distinguishing between targets. The if-lerp target should be x, whatever x is, and the else-lerp target should be whatever you actually started at. This “whatever you started at” is actually something I saved as a const so that it could be referenced as the else-lerp target without the possibility of losing it. That works because your starting point isn’t going to be dependent on the aim multiplier of the gun you’re using.

So, the pseudocode would look like this (pseudocode because my own code is drastically longer due to changes I made):

const starting_fov = cam.fov
~
if:
  cam.fov = lerp(cam.fov, cam_fov_zoom_target * aim_multiplier, delta * mn)
  #same for each cam
else:
  cam.fov = lerp(cam.fov, starting_fov, delta * mn) #mn = magic number
  #same for each cam

This should fix it as that matches what I did in my own project, but more importantly, hopefully the explanation makes sense and explains what was happening =)

Also, because your target was being lerped, you will probably have to change some of your numbers (aim multipliers, and possibly mns) to get the desired result.

1 Like

So taking your advice I rewrote and made some changes and it seems to be working as I wanted with exported variables to make changes on the fly.

@export_category("Camera Settings")
@export var aim_fov := 45
@export var view_zoom_in := 20
@export var view_zoom_out := 25
@export var weapon_zoom_in := 14
@export var weapon_zoom_out := 22
func _process(delta: float) -> void:
	if Input.is_action_pressed("aim"):
		smooth_camera.fov = lerp(smooth_camera.fov, aim_fov * aim_multiplier, view_zoom_in * delta)
		weapon_camera.fov = lerp(weapon_camera.fov, aim_fov * aim_multiplier, weapon_zoom_in * delta)
	else:
		smooth_camera.fov =  lerp(smooth_camera.fov, smooth_camera_fov, delta * view_zoom_out)
		weapon_camera.fov =  lerp(weapon_camera.fov, weapon_camera_fov, delta * weapon_zoom_out)

Still waiting to see how we Bram fixed the impact particles being at the last hit location, I have tried and just can’t pinpoint what is wrong, I guess my debugging is weak lol.

1 Like

It comes with practice. The particles problem is actually happening because a condition is missing. You don’t want to see impact particles on every shot after all, right? =)

Well I don’t know if this is the best use of this but it worked…
image

Maybe I am not terrible, I just need a nudge lol.

1 Like

Yeah, that’s the right idea! It can indeed be done a little more precisely in that you can check if the collider is returning anything at all, but now that you’ve discovered this, I will leave that part for Bram to explain. Nothing wrong with doing it this way though.

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

Privacy & Terms