提问人:Taimen 提问时间:11/17/2023 更新时间:11/17/2023 访问量:21
Godot “无效的获取索引'转换'(在基础上;无')。错误
Godot "Invalid get index 'transform' (on base; 'NIL'). Error
问:
我正在遵循一些教程,typiyng与教程中的人完全相同,但是当我尝试运行项目时,我得到了“无效的获取索引'transform'(在基础上;NIL') 错误',我真的不知道为什么
extends CharacterBody3D
const SPEED = 5.0
const JUMP_VELOCITY = 4.5
@export var TILT_LOWER_LIMIT := deg_to_rad(-90.0)
@export var TILT_UPPER_LIMIT := deg_to_rad(90.0)
@export var CAMERA_CONTROLLER : Camera3D
var _mouse_input : bool = false
var _mouse_rotation : Vector3
var _rotation_input : float
var _tilt_input : float
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
func _input(event):
if event.is_action_pressed("exit"):
get_tree().quit()
func _unhandled_input(event):
_mouse_input = event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED
if _mouse_input:
_rotation_input = -event.relative.x
_tilt_input = -event.relative.y
print(Vector2(_rotation_input,_tilt_input,))
func _update_camera(delta):
# Rotate camera using euler rotation
_mouse_rotation.x += _tilt_input * delta
_mouse_rotation.x = clamp(_mouse_rotation.x, TILT_LOWER_LIMIT, TILT_UPPER_LIMIT)
_mouse_rotation.y += _rotation_input * delta
CAMERA_CONTROLLER.transform.basis = Basis.from_euler(_mouse_rotation)
CAMERA_CONTROLLER.rotation.z = 0.0
_rotation_input = 0.0
_tilt_input = 0.0
func _ready():
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y -= gravity * delta
_update_camera(delta)
# Handle Jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var input_dir = Input.get_vector("move_left", "move_right", "move_forward", "move_backward")
var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if direction:
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.z = move_toward(velocity.z, 0, SPEED)
move_and_slide()
如果需要,这里是脚本和场景树
我第三次检查了我的代码,但看不到任何问题,我确定我犯了一些愚蠢的语法错误,但又看不出在哪里。 此外,如果我删除所有引用CAMERA_CONTROLLER项目的内容,并且CAMERA_CONTROLLER引用 Camera3D,以及其中的某些内容,脚本就看不到,
我尝试重新输入脚本两次,但问题仍然存在,将重置相机 3d 位置更改为 0 并仅使用 CameraController 移动它。
答: 暂无答案
评论
CAMERA_CONTROLLER
Camera3D
CharacterBody3D
CAMERA_CONTROLLER
Camera3D
CharacterBody3D
Camera3D