Godot:2D Raycast的发展方向相反

问题描述

刚开始学习Godot并遇到2D Raycasts左移而不是右或右移而不是左的问题。

所以我有一个基本的自上而下的2D游戏,播放器上有光线投射。向上和向下面向都可以很好地工作,只是左右造成了头痛。代码看起来不错,所以这就是为什么我在这里

onready var rayCast = get_node("RayCast2D")

func _physics_process(delta):

    vel = Vector2()

    # inputs
    if Input.is_action_pressed("move_up"):
        vel.y -= 1
        facingDir = Vector2(0,-1)
    
    if Input.is_action_pressed("move_down"):
        vel.y += 1
        facingDir = Vector2(0,1)
    
    if Input.is_action_pressed("move_left"):
        vel.x -= 1
        facingDir = Vector2(1,0)
    
    if Input.is_action_pressed("move_right"):
        vel.x += 1
        facingDir = Vector2(-1,0)
    

    vel = vel.normalized()

    move_and_slide(vel * moveSpeed)

    manage_animations()

func manage_animations ():

    if vel.x > 0:
        play_animation("MoveRight")
    elif vel.x < 0:
        play_animation("MoveLeft")
    elif vel.y < 0:
        play_animation("MoveUp")
    elif vel.y > 0:
        play_animation("MoveDown")
    elif facingDir.x == -1:
        play_animation("IdleRight")
    elif facingDir.x == 1:
        play_animation("IdleLeft")
    elif facingDir.y == -1:
        play_animation("IdleUp")
    elif facingDir.y == 1:
        play_animation("IdleDown")

func _process(delta):

    if Input.is_action_just_pressed("interract"):
        try_interact()

func try_interact():

    rayCast.cast_to = facingDir * interactDist

    if rayCast.is_colliding():
        if rayCast.get_collider() is KinematicBody2D:
            rayCast.get_collider().take_damage(damage)
        elif rayCast.get_collider().has_method("on_interact"):
            rayCast.get_collider().on_interact(self)

我试图在左右移动中更改Vector2的x和y变量,并且在可行的同时,步行也变得一团糟。

有什么想法吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...