我正在尝试使用GDScript在Godot中创建可播放的精灵。我的角色向左和向右移动,然后在没有任何输入被按下时停下来。
但是,我不想让死神停下来,而是要让精灵放慢速度。我该怎么写?
extends KinematicBody2D
var motion = Vector2()
func _physics_process(delta):
if Input.is_action_pressed("ui_right"):
motion.x = 250
elif Input.is_action_pressed("ui_left"):
motion.x = -250
else:
motion.x = 0
move_and_slide(motion)
pass
答案 0 :(得分:0)
好吧,我知道了:
extends KinematicBody2D
var motion = Vector2()
func _physics_process(delta):
if Input.is_action_pressed("ui_right"):
motion.x = 250
elif Input.is_action_pressed("ui_left"):
motion.x = -250
else:
motion.x = motion.x * .9
move_and_slide(motion)
pass
答案 1 :(得分:0)
一种非常简单的方法是降低每次更新的速度-一种从物理阻力中借用的概念。
function retrieveCategories(active) {
document.body.style.cursor = "wait";
var data = JSON.stringify({
"active": active
});
console.log(data);
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function () {
if (this.readyState === 4 && this.status == 403) {
logout();
document.body.style.cursor = "initial";
} else if (this.readyState === 4 && this.status != 200) {
document.body.innerHTML = 'Error: ' + this.responseText;
document.body.style.cursor = "initial";
} else if (this.readyState === 4 && this.status == 200) {
authToken = this.getResponseHeader('Authorization');
categories = JSON.parse(this.responseText);
document.body.style.cursor = "initial";
}
});
xhr.open("POST", "http://127.0.0.1:7071/retrievecategories");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Authorization", authToken);
xhr.send(data);
}
同样,这非常简单。如果要提供某些保证(例如播放器应在6帧后完全停止播放),则调整拖曳值可能会非常挑剔,尤其是在处理解锁的fps时。
在这种情况下,不要有单个extends KinematicBody2D
var motion = Vector2()
func _physics_process(delta):
if Input.is_action_pressed("ui_right"):
motion.x = 250
elif Input.is_action_pressed("ui_left"):
motion.x = -250
else:
motion.x *= 0.95 # slow down by 5% each frame - play with this value
# i.e. make it dependent on delta to account for different fps
# or slow down faster each frame
# or clamp to zero under a certain threshold
move_and_slide(motion)
pass
向量,而是要有motion
和last_motion
向量,并在它们之间进行内插,以便每帧获得当前的target_motion
向量(您也可以这样做来加速)。这需要您考虑状态转换,例如“当玩家停止按下按钮时”,启动计时器以跟踪插值的起始帧和目标帧之间的进度,等等。。。选择的kind of interpolation function将影响动作对玩家的反应速度或反应迟缓。
下面是使用线性插值在0.2秒内减速的示例:
motion
会感觉到线性减速,但是您可以轻松地将其替换为任何其他功能,例如尝试三次以加快减速,从而降低响应速度:enum MotionState {IDLE, MOVING, DECELERATING}
extends KinematicBody2D
var state = IDLE
var last_motion = Vector2()
var time_motion = 0.0
func _physics_process(delta):
var motion = Vector2();
if Input.is_action_pressed("ui_right"):
motion.x = 250
state = MOVING
last_motion = motion
elif Input.is_action_pressed("ui_left"):
motion.x = -250
state = MOVING
last_motion = motion
elif state == IDLE
motion.x = 0
else:
if state == MOVING:
# start a smooth transition from MOVING through DECELERATING to IDLE
state = DECELERATING
# last_motion already holds last motion from latest MOVING state
time_motion = 0.2
# accumulate frame times
time_motion -= delta
if time_motion < 0.0:
# reached last frame of DECELERATING, transitioning to IDLE
state = IDLE
motion.x = 0
else:
var t = (0.2 - time_motion) / 0.2 # t goes from 0 to 1
# we're linearly interpolating between last_motion.x and 0 here
motion.x = (1 - t) * last_motion.x
move_and_slide(motion)
pass