How can I smoothly Rotate the Camera up 15 Degrees on Button Press and smoothly back to 0 on Button Release

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Retroarchy

I’m trying to rotate the player_head spatial (which has a camera as a child) up by 15 degrees when I press the look up button and back down when I release it. I have that system working but it just snaps to 15 degrees and back, instead of interpolating between the values. I’ve tried using the lerp functions with it and nothing is working out so far. I have no idea how I could do it with Tweens since I’m really unfamiliar with Tween Node.

Here’s the code for simply looking up and back down 15 degrees.

func _physics_process(delta):

if Input.is_action_just_pressed("player_look_up"):
		rotation_degrees.x = 15

if Input.is_action_just_released("player_look_up"):
	    rotation_degrees.x = 0

tween node

'ts like magic
also I hate using it lol

HalfAsleepSam | 2022-11-19 05:33

at least your hatred still compels you to
never have and probably never will since the lerp() and move_toward funcs exsits

Wakatta | 2022-11-19 17:09

:bust_in_silhouette: Reply From: TheIronGod

There’s a few ways you can get what you want.

  1. You can use an AnimationPlayer node and then where you change the rotation value in your code just play that animation.

  2. You can us a tween node, and execute that where you change the rotation value in your code.

Those will get you the best looking result but you could also change the value incrementally using a loop.

var lookUpMaxRotation = 15
lookUpSpeed = 1
if Input.is_action_just_pressed("player_look_up"):
     while rotation_degrees.x < lookUpMaxRotation:
           rotation_degrees.x += lookUpSpeed
if Input.is_action_just_released("player_look_up"):
     while rotation_degrees.x > 0
           rotation_degrees.x -= lookUpSpeed

I didn’t test that chunk of code but it should work, and you could adjust the speed by changing the lookUpSpeed.

Thank you for this, but this just seems to snap the camera up again instead of incrementally doing it. I’ve adjusted the speed to various values and it still just snaps up and down.

Retroarchy | 2022-11-19 14:25

:bust_in_silhouette: Reply From: Wakatta

Maybe something like this

var rotation_speed = 0.0

func _physics_process(delta):
	if Input.is_action_just_pressed("ui_up"):
		rotation_speed = 0.2
		
	if Input.is_action_just_released("ui_up"):
		rotation_speed = -0.2
		
    #prevent rotation beyond limits
	rotation_degrees.x = clamp(rotation_degrees.x + rotation_speed, 0, 15)

Actually looking at it again maybe replace the last line with

if rotation_speed:
	#prevent rotation beyond limits
	rotation_degrees.x = clamp(rotation_degrees.x + rotation_speed, 0, 15)
	#not necessarily needed but prevents running this block of code
	if rotation_degrees.x == 0:
		rotation_speed = 0