【问题标题】:Godot make item follow mouseGodot 使项目跟随鼠标
【发布时间】:2019-01-31 18:01:42
【问题描述】:

我正在 Godot 3.0 中制作 2D 平台游戏,我想让玩家使用鼠标瞄准(类似于泰拉瑞亚中的弓和枪)投掷/射击物品。我该怎么做呢?我正在使用 gdscript。

【问题讨论】:

    标签: 2d mouse godot gdscript


    【解决方案1】:

    您可以使用look_at() 方法(Node2DSpatial 类)和get_global_mouse_position()

    func _process(delta):
        SomeNode2DGun.look_at(get_global_mouse_position())
    

    【讨论】:

      【解决方案2】:

      从鼠标位置减去玩家位置向量,你会得到一个从玩家指向鼠标的向量。然后你可以使用向量的angle 方法来设置你的射弹的角度并标准化向量并将其缩放到所需的长度以获得速度。

      extends KinematicBody2D
      
      var Projectile = preload('res://Projectile.tscn')
      
      func _ready():
          set_process(true)
      
      func _process(delta):
          # A vector that points from the player to the mouse position.
          var direction = get_viewport().get_mouse_position() - position
      
          if Input.is_action_just_pressed('ui_up'):
              var projectile = Projectile.instance()  # Create a projectile.
              # Set the position, rotation and velocity.
              projectile.position = position
              projectile.rotation = direction.angle()
              projectile.vel = direction.normalized() * 5  # Scale to length 5.
              get_parent().add_child(projectile)
      

      我在本例中使用KinematicBody2D 作为Projectile.tscn 场景并使用move_and_collide(vel) 移动它,但您也可以使用其他节点类型。另外,调整碰撞层和遮罩,使弹丸不会与玩家发生碰撞。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多