Rotate object when a key is pressed

4.3

dumb question but i couldnt find a way to solve this
im trying to make a weapon switching system but i want to use as little animations as possible so my plan is to simply lower the gun when unequipping it and raise it when equipping

however im having a problem where i cant do it smootly, for some reason lerp isnt working

extends Node3D

@export var bullets: int = 20
@export var weapon = curWeapon.shotgun #equipped weapon
@onready var hand = $hand

enum curWeapon
{
	shotgun,
	pistol,
	none
}

func _input(event):
	if Input.is_action_just_pressed("weapon1"):
		weapon = curWeapon.shotgun
		
	if Input.is_action_just_pressed("weapon2"):
		weapon = curWeapon.pistol

	if Input.is_action_just_pressed("weapon3"):
		if weapon != curWeapon.none:
			weapon = curWeapon.none
			#rotate hand z to -45 here

if you want to use lerp you have to use it in a looping function such as process() since lerp only returns one value. I would use tweens though since they make your life a whole lot easier (basically animations by code).

Godot doc:
https://docs.godotengine.org/en/stable/classes/class_tween.html

YT Tutorial:
https://m.youtube.com/watch?v=GN_HMhvYnYA&pp=ygUjZ29kb3Qgcm90YXRlIGFuIG9iamVjdCB1c2luZyB0d2VlbnM%3D

1 Like

You can use animation player for it which is the easiest way, so just play the animation in the codes when any weapon is equal.

thank you, was able to get it working like this

extends Node3D

@export var bullets: int = 20
@export var weapon = curWeapon.none #equipped weapon

@onready var hand = $hand
@onready var shotgun: Node3D = $hand/shotgun
@onready var pistol: Node3D = $hand/pistol

var unqTweenPos
var unqTweenRot
var eqpTweenPos
var eqpTweenRot

enum curWeapon
{
	shotgun,
	pistol,
	none
}

func _process(delta):
	if Input.is_action_just_pressed("weapon1"):
		if weapon == curWeapon.pistol:
			unequip(delta)
			await unqTweenPos.finished
		weapon = curWeapon.shotgun
		shotgun.visible = true
		pistol.visible = false
		equip(delta)
		
	if Input.is_action_just_pressed("weapon2"):
		if weapon == curWeapon.shotgun:
			unequip(delta)
			await unqTweenPos.finished
			
		weapon = curWeapon.pistol
		shotgun.visible = false
		pistol.visible = true
		equip(delta)
		
	if Input.is_action_just_pressed("weapon3") && weapon != curWeapon.none:
		unequip(delta)
		weapon = curWeapon.none
		
		await unqTweenPos.finished
		shotgun.visible = false
		pistol.visible = false


func unequip(delta):
	unqTweenRot = get_tree().create_tween().set_ease(Tween.EASE_OUT).set_trans(Tween.TRANS_LINEAR)
	unqTweenPos = get_tree().create_tween().set_ease(Tween.EASE_OUT).set_trans(Tween.TRANS_LINEAR)

	unqTweenRot.tween_property(hand, "rotation:z",deg_to_rad(-45), delta * 4)
	unqTweenPos.tween_property(hand, "position:y", -2, delta * 8)

func equip(delta):
	eqpTweenRot = get_tree().create_tween().set_ease(Tween.EASE_OUT).set_trans(Tween.TRANS_LINEAR)
	eqpTweenPos = get_tree().create_tween().set_ease(Tween.EASE_OUT).set_trans(Tween.TRANS_LINEAR)
	
	eqpTweenRot.tween_property(hand, "rotation:z",deg_to_rad(0), delta * 4)
	eqpTweenPos.tween_property(hand, "position:y", 0, delta * 8)