为什么玩家不会摔倒?

huangapple go评论69阅读模式
英文:

Why doesn't the player fall down?

问题

我正在使用Godot制作我的第一个手机游戏,所以我经验不足。这是一个横版平台游戏。当玩家与敌人碰撞时,我希望玩家掉下来并死亡。我尝试了几天,但总是遇到一些不同的问题,我不明白我做错了什么。有人可以帮我吗?我给你玩家的代码;这样玩家尽管设置了velocity.y = 500,但并没有掉下来,而是做了一种跳跃的动作。哪里有问题?非常感谢。

玩家代码:

extends KinematicBody2D

onready var rayS = get_node("rayL")
onready var rayD = get_node("rayR")
onready var sprite = get_node("AnimatedSprite")

var alive = true

signal dead

const WALK_FORCE = 600
const WALK_MAX_SPEED = 300
const STOP_FORCE = 1300
const JUMP_SPEED = 700
const gravity = 1100.0

var velocity = Vector2()

func _physics_process(delta):
    var force = Vector2(0, gravity)
    
    var walk_left = Input.is_action_pressed("move_left") and alive
    var walk_right = Input.is_action_pressed("move_right") and alive
    var jump = Input.is_action_pressed("jump") and alive
    
    var walk = WALK_FORCE * (Input.get_action_strength("move_right") - Input.get_action_strength("move_left"))
    
    if abs(walk) < WALK_FORCE * 0.2:
        velocity.x = move_toward(velocity.x, 0, STOP_FORCE * delta)
    else:
        velocity.x += walk * delta
    
    velocity.x = clamp(velocity.x, -WALK_MAX_SPEED, WALK_MAX_SPEED)

    velocity.y += gravity * delta

    velocity = move_and_slide_with_snap(velocity, Vector2.DOWN, Vector2.UP)

    if is_on_floor() and Input.is_action_just_pressed("jump"):
        jumping()
    
    var on_floor = rayL.is_colliding() or rayR.is_colliding()
    
    if walk_right:
        sprite.set_flip_h(false)
    if walk_left:
        sprite.set_flip_h(true)
    
    if (walk_left or walk_right) and on_floor:
        sprite.play()
    elif (walk_left or walk_right):
        sprite.stop()
        sprite.set_frame(3)
    else:
        sprite.stop()
        sprite.set_frame(1)
    
    if position.y > 900: dying()

func _on_Area2D_body_entered(body):
    if not alive: return
    jumping()
    body.destroy()

func jumping():
    velocity.y = -JUMP_SPEED

func _on_Area2D2_body_entered(body):
    if not alive: return
    dying()

func dying():
    if not alive: return
    alive = false
    velocity.y = -500
    collision_mask -= 2
    emit_signal("dead")
英文:

I'm making my first mobile game on godot so I'm very inexperienced. It is a platformer. I would like the player, when he collides with the enemy, to fall down and die. I've been trying to do it for days, but it always gives me some different problem and I don't understand what I'm doing wrong. Could anyone help me please? I leave you the player code; in this way the player, despite having set velocity.y=.500, does not fall, but makes a sort of jump. What's wrong? Thank you all in advance.

PLAYER CODE:

extends KinematicBody2D
onready var rayS = get_node(&quot;rayL&quot;)
onready var rayD = get_node(&quot;rayR&quot;)
onready var sprite = get_node(&quot;AnimatedSprite&quot;)
var alive = true
signal dead
const WALK_FORCE = 600
const WALK_MAX_SPEED = 300
const STOP_FORCE = 1300
const JUMP_SPEED = 700
const gravity = 1100.0
var velocity = Vector2()
func _physics_process(delta):
var force = Vector2(0, gravity)
var walk_left = Input.is_action_pressed(&quot;move_left&quot;) and alive
var walk_right = Input.is_action_pressed(&quot;move_right&quot;) and alive
var jump = Input.is_action_pressed(&quot;jump&quot;) and alive
# Horizontal movement code. First, get the player&#39;s input.
var walk = WALK_FORCE * (Input.get_action_strength(&quot;move_right&quot;) - Input.get_action_strength(&quot;move_left&quot;))
# Slow down the player if they&#39;re not trying to move.
if abs(walk) &lt; WALK_FORCE * 0.2:
# The velocity, slowed down a bit, and then reassigned.
velocity.x = move_toward(velocity.x, 0, STOP_FORCE * delta)
else:
velocity.x += walk * delta
# Clamp to the maximum horizontal movement speed.
velocity.x = clamp(velocity.x, -WALK_MAX_SPEED, WALK_MAX_SPEED)
# Vertical movement code. Apply gravity.
velocity.y += gravity * delta
# Move based on the velocity and snap to the ground.
velocity = move_and_slide_with_snap(velocity, Vector2.DOWN, Vector2.UP)
# Check for jumping. is_on_floor() must be called after movement code.
if is_on_floor() and Input.is_action_just_pressed(&quot;jump&quot;):
jumping()
var on_floor = rayL.is_colliding() or rayR.is_colliding()
if walk_right:
sprite.set_flip_h(false)
if walk_left:
sprite.set_flip_h(true)
if (walk_left or walk_right) and on_floor:
sprite.play()
elif (walk_left or walk_right):
sprite.stop()
sprite.set_frame(3)
else:
sprite.stop()
sprite.set_frame(1)
if position.y &gt; 900: dying()
func _on_Area2D_body_entered(body):
if not alive: return
jumping()
body.destroy()
func jumping():
velocity.y = -JUMP_SPEED
func _on_Area2D2_body_entered(body):
if not alive: return
dying()
func dying():
if not alive: return
alive = false
velocity.y = -500
collision_mask -= 2
emit_signal(&quot;dead&quot;)

答案1

得分: 1

在二维空间中,上方方向在y轴上是负方向。换句话说,Y轴指向下方。

因此,设置速度如下:

velocity.y = -500

会导致向上运动,而不是向下运动。


附注

您可以通过查看Vector2.UPVector2.DOWN的定义来确认Y轴上的负方向是向上,Y轴上的正方向是向下(文档)。

要使玩家角色向下移动,请给它一个向下的速度。重申一下,这在Y轴上是正方向。例如:

velocity.y = 500

重申一下:上方方向在y轴上是负方向,因此带有负Y分量的速度会使物体向上移动。

英文:

In 2D, the UP direction is negative on the y axis. Said another way, the Y axis is pointing downwards.

Thus, setting the velocity like this:

velocity.y = -500

Will result in upward motion, not downwards.


Addendum:

You can confirm that negative on the Y axis is upwards, and positive on the Y axis is downwards by looking at the definitions of Vector2.UP and Vector2.DOWN (documentation).

To make the player character move downwards, give it a downwards velocity. Which, to reiterate, is positive on the Y axis. For example:

velocity.y = 500

To reiterate: the UP direction is negative on the y axis and thus a velocity with a negative Y component moves upwards.

huangapple
  • 本文由 发表于 2023年2月19日 15:00:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75498500.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定