0 votes

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
Godot version 3.5
in Engine by (12 points)
edited by

tween node

'ts like magic
also I hate using it lol

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

2 Answers

0 votes

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.

by (123 points)

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.

0 votes

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
by (6,870 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.