我正在用Godot3.0制作一个2D平台,我想让玩家使用鼠标来投掷/射击物品(类似于Terraria中的弓和枪)。我该怎么做呢?我正在使用gdscript。
发布于 2018-09-11 08:15:43
您可以使用look_at()方法(Node2D和Spatial类)和get_global_mouse_position()
func _process(delta):
SomeNode2DGun.look_at(get_global_mouse_position())发布于 2018-08-27 16:03:07
从鼠标位置减去玩家位置向量,你将得到一个从玩家指向鼠标的向量。然后,您可以使用矢量的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)移动它,但您也可以使用其他节点类型。此外,调整碰撞层和遮罩,使投射物不会与播放器碰撞。
https://stackoverflow.com/questions/52018293
复制相似问题