Trying to mimic SpringArm3D since it doesn't seem to work

Godot Version

v4.3.stable.official [77dcf97d8]

Question

My game keeps crashing and I’m not understanding why. I randomly get the error “Invalid access to property or key ‘position’ on a base object of type ‘InputEventKey’”.

Here’s my code:

extends CharacterBody3D

@onready var player := %Player
@onready var right_ray := %RightRayCast3D
@onready var left_ray := %LeftRayCast3D2

var we_are_colliding := false


func _physics_process(delta):
	velocity = player.velocity
	move_and_slide()
	
	if position != player.position:
		
		position = lerp(position,player.position,1*delta)
	
	if right_ray.is_colliding():
		rotation_degrees += Vector3(0,-1,0)
		we_are_colliding = true
	elif left_ray.is_colliding():
		rotation_degrees += Vector3(0,1,0)
		we_are_colliding = true
	else:
		we_are_colliding = false


func _input(event):
	var org_pos := Vector2(300,600)
	if Input.is_action_just_pressed("move_camera"):
		org_pos = event.position

	if Input.is_action_pressed("move_camera"):
		if event is InputEventMouseMotion:
			var rot = org_pos-event.position
			
			if !we_are_colliding:
				org_pos.x = deg_to_rad(org_pos.x)
				rot.x = deg_to_rad(rot.x)
				rotation.y = lerp_angle(org_pos.x,rot.x,1)

Help would be appreciated!

input events do not have “position”, especially key-events. Try adding some type hints to get better auto-complete, if your line numbers are green then the line is “type-safe” meaning you should not get invalid access errors. You can enable type hints in your editor settings, they should be enabled by default in 4.3

func _input(event: InputEvent) -> void:
1 Like

Thanks, for the explanation!

I guess I was just confused by this page:

func _input(event):
	# Mouse in viewport coordinates.
	if event is InputEventMouseButton:
		print("Mouse Click/Unclick at: ", event.position)
	elif event is InputEventMouseMotion:
		print("Mouse Motion at: ", event.position)

	# Print the size of the viewport.
	print("Viewport Resolution is: ", get_viewport().get_visible_rect().size)

And also, when you print(event) it displays a position, just not always, which is why the bug didn’t happen instantly.

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