英文:
Godot smooth 2d player movement script errors
问题
I'll provide translations for the code and the error messages:
const SPEED = 200
const FRICTION = 5
const DASH_SPEED = 400
const DASH_DURATION = 0.1
const DASH_COOLDOWN = 1.5
var velocity = Vector2.ZERO
var dashTimer = 0
var canDash = true
func _physics_process(delta: float) -> void:
# 玩家移动
var inputVector = Vector2.ZERO
if Input.is_action_pressed("ui_up"):
inputVector.y -= 1
if Input.is_action_pressed("ui_down"):
inputVector.y += 1
if Input.is_action_pressed("ui_left"):
inputVector.x -= 1
if Input.is_action_pressed("ui_right"):
inputVector.x += 1
# 应用摩擦
if inputVector == Vector2.ZERO:
velocity = velocity.linear_interpolate(Vector2.ZERO, FRICTION * delta)
else:
velocity += inputVector.normalized() * SPEED * delta
# 执行冲刺
if canDash and Input.is_action_just_pressed("dash"):
var dashDirection = get_global_mouse_position() - position
var dashDistance = dashDirection.length()
if dashDistance > 0:
dashDirection = dashDirection.normalized()
velocity = dashDirection * DASH_SPEED
dashTimer = DASH_DURATION
canDash = false
# 应用冲刺冷却
if not canDash:
dashTimer -= delta
if dashTimer <= 0:
canDash = true
# 移动玩家
linear_velocity = velocity
错误信息:
Line 9: 重定义成员 "velocity"(原始定义在本机类 'CharacterBody2D')
Line 48: 当前作用域中未声明标识符 "linear_velocity"
请注意,代码中的引号已根据代码规范更正为双引号。
英文:
I'm trying to program top-down player movement for my game, and im getting errors that are probably a really simple fix, but im new to godot, and need help. my code is:
const SPEED = 200
const FRICTION = 5
const DASH_SPEED = 400
const DASH_DURATION = 0.1
const DASH_COOLDOWN = 1.5
var velocity = Vector2.ZERO
var dashTimer = 0
var canDash = true
func _physics_process(delta: float) -> void:
# Player movement
var inputVector = Vector2.ZERO
if Input.is_action_pressed("ui_up"):
inputVector.y -= 1
if Input.is_action_pressed("ui_down"):
inputVector.y += 1
if Input.is_action_pressed("ui_left"):
inputVector.x -= 1
if Input.is_action_pressed("ui_right"):
inputVector.x += 1
# Apply friction
if inputVector == Vector2.ZERO:
velocity = velocity.linear_interpolate(Vector2.ZERO, FRICTION * delta)
else:
velocity += inputVector.normalized() * SPEED * delta
# Perform dash
if canDash and Input.is_action_just_pressed("dash"):
var dashDirection = get_global_mouse_position() - position
var dashDistance = dashDirection.length()
if dashDistance > 0:
dashDirection = dashDirection.normalized()
velocity = dashDirection * DASH_SPEED
dashTimer = DASH_DURATION
canDash = false
# Apply dash cooldown
if not canDash:
dashTimer -= delta
if dashTimer <= 0:
canDash = true
# Move the player
linear_velocity = velocity
the two errors are:
Line 9:Member "velocity" redefined (original in native class 'CharacterBody2D')Line 48:Identifier "linear_velocity" not declared in the current scope.
I've tried looking around on tutorials, and they all seem to use this method, any help?
答案1
得分: 3
-
linear_velocity
是刚体的一个属性,你没有在使用一个刚体。所以不要这样做:
linear_velocity = velocity
-
velocity
是角色刚体的一个属性,你正在使用一个角色刚体,你不需要定义速度。所以,移除这行:
var velocity = Vector2.ZERO
注:角色刚体是Godot 4中取代Godot 3运动刚体的新类型,你不需要自己定义速度。
修复后,你会发现你的角色没有移动。这是因为移动角色刚体的责任由你负担(不像刚体,刚体的移动由Godot的物理系统负责)。
使你的角色刚体移动的常用方法是调用 move_and_slide
。在Godot 3中,你需要将速度作为参数传递,但在Godot 4中,速度是一个属性,你只需在调用 move_and_slide
之前设置一些时间。
英文:
-
linear_velocity
is a property of rigid bodies, you are not using a rigid body.So don't do this:
linear_velocity = velocity
-
velocity
is a property of character bodies, you are using a character body, you don't need to define velocity.So, remove this:
var velocity = Vector2.ZERO
Note: character bodies are the Godot 4 replacement for Godot 3 kinematic bodies, for which you had to define your own velocity.
I'm guessing you have been mixing tutorials.
After fixing that you will find that your character does not move. This is because you are responsible for moving character bodies (unlike rigid bodies, for which the responsibility of moving them falls to the physics system of Godot).
The usual way to make to make your character body move is by calling move_and_slide
. In Godot 3 you would have to pass the velocity as an argument, but in Godot 4 the velocity is a property, which you just set some time before calling move_and_slide
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论