戈多版本 4.2.2
问题 你好,我对戈多非常陌生,所以谢谢你帮助我。我正在开发一个 2d 平台游戏,我试图在游戏中添加一个破折号,玩家向鼠标移动,我得到了它的工作,但是当我对角线破折号时,它似乎走了大约一半的距离,就像我直线上升一样,向下、向左或向右,这是脚本:
extends CharacterBody2D
@onready var sprite_0001_png = $"Sprite-0001_png"
const dash_time = 0.1
const dash_speed = 600
var dashing = false
var dash_charge = 1
var movement_lock = false
const SPEED = 250
const JUMP_VELOCITY = -600
var death = 0
var player_pos
var mouse_pos
func _process(delta):
mouse_pos = get_global_mouse_position()
player_pos = get_global_position()
pass
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
func player_die():
velocity = Vector2(0, 0)
movement_lock = true
await get_tree().create_timer(1).timeout
dashing = true
get_tree().reload_current_scene()
movement_lock = false
func _physics_process(delta):
dash()
if not movement_lock == true and not is_on_floor():
velocity.y += gravity * delta
if is_on_floor() and dash_charge < 1:
dash_charge += 1
# Handle jump.
if not movement_lock == true and Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
if not movement_lock == true:
var direction = Input.get_axis("move_left", "move_right")
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, 15)
var isright = velocity.x < 0
sprite_0001_png.flip_h= isright
move_and_slide()
func dash():
print(mouse_pos)
if dash_charge > 0 and not dashing:
var dir
if Input.is_action_just_pressed("dash"):
velocity = Vector2(0, 0)
movement_lock = true
dash_charge -= 1
dashing = true
dir = (mouse_pos - player_pos)
dir = dir.normalized()
velocity = dir * dash_speed
dash_charge -= 1
dashing = true
await get_tree().create_timer(dash_time).timeout
dashing = false
movement_lock = false
目前,您已经标准化了 dir 变量,这使得角色的移动距离恒定,因此它充当您可以冲刺的半径。至少我认为我不完全知道归一化向量是如何工作的,但在 3D 中,归一化向量有一个可以指向的可能方向的 3D 范围,所有方向的力均为 1。您需要做的是删除归一化部分并设置您希望玩家移动的 x 和 y 的最大距离。之后应该就可以了。如果不认识的话 ́\ (ツ) /́