Make door open the direction the player is pointing

Godot Version

v4.2.1

Question

Can anyone help me make that door open the direction the player is pointing… So far I got this but it doesn’t work at all. I tried so many things I won’t be abole to tell what I tried. 3 hours on a door opening issue is a bit long :rofl:

class_name Door
extends InteractionSwitchable

@onready var door_body = $DoorBody
@onready var animation_player = $AnimationPlayer


var last_anim = "door_open_front"
func interact(from: Node):
	var state = super(from)
	var animation = "door_open_front"
	
	if from is Player:
		var d = self.rotation
		var m = from.rotation
		var dm = d.dot(m)
		print(dm)
		
		if dm < 0:
			animation = "door_open_front"
		else:
			animation = "door_open_back"
	
	if state:
		animation_player.play(animation)
		last_anim = animation
	else:
		animation_player.play(last_anim, -1, -1.0, true)

I guess this is for 3D nodes? A Node3D’s rotation gives you three euler angles, so using the dot product here doesn’t make sense at all. Maybe you rather want the forward vector, which you can get via global_basis.z.

2 Likes

Omg I feel so stupid! This makes way more sense!

Final code for any beginner to use if they didn’t understand how to fix!

class_name Door
extends InteractionSwitchable

@onready var door_body = $DoorBody
@onready var animation_player = $AnimationPlayer


var last_anim = "door_open_front"
func interact(from: Node):
	var state = super(from)
	var animation = "door_open_front"
	
	if from is Player:
		var d = self.global_basis.z # Error was here
		var m = from.global_basis.z # And here
		var dm = d.dot(m)
		print(dm)
		
		if dm < 0:
			animation = "door_open_front"
		else:
			animation = "door_open_back"
	
	if state:
		animation_player.play(animation)
		last_anim = animation
	else:
		animation_player.play(last_anim, -1, -1.0, true)

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