我想编辑下面的跳转代码。
我想让角色跳得更快(减少jump_count),但是在jump_count的每次下降中,我希望跳的最大高度保持不变。没有任何变化。你能帮帮我吗?
进一步解释
jump_count = 10
jump_height = 0.6
jump= -36.999999999999986的最大高度
jump_count =5
jump_height =?
jump= -36.999999999999986的最大高度
jump_count =3
jump_height =?
jump= -36.999999999999986的最大高度
代码:
class Character:
def __init__(self):
# Position
self.x = 70
self.y = 194
# Move
self.velocity = 10
# jump:
self.is_jump = False
# Jump count
self.jump_count_range = 10
self.jump_count = self.jump_count_range
# Jump height
self.constant_jump_height_range = 0.6
self.jump_height = self.constant_jump_height_range
def Jump(self):
if self.is_jump:
if self.jump_count >= -1 * self.jump_count_range:
neg = 1
if self.jump_count < 0:
neg = -1
self.y -= (self.jump_count ** 2) * self.jump_height * neg
self.jump_count -= 1
else:
self.is_jump = False
self.jump_count = self.jump_count_range发布于 2020-12-13 14:35:53
只需添加类似max_jump_height的内容,如下所示
class Character:
def __init__(self):
# Position
self.x = 70
self.y = 194
# Move
self.velocity = 10
# jump:
self.is_jump = False
# Jump count
self.jump_count_range = 10
self.jump_count = self.jump_count_range
# Jump height
self.constant_jump_height_range = 0.6
self.jump_height = self.constant_jump_height_range
self.max_jump_height = -36.999999999999986
def Jump(self):
if self.is_jump:
if self.jump_count >= -1 * self.jump_count_range:
neg = 1
if self.jump_count < 0:
neg = -1
if self.max_jump_height < (self.jump_count ** 2) * self.jump_height * neg:
return
else:
self.y -= (self.jump_count ** 2) * self.jump_height * neg
self.jump_count -= 1
else:
self.is_jump = False
self.jump_count = self.jump_count_range发布于 2020-12-13 14:38:34
尝试添加以下内容:
float JumpVelocity,
float JumpDampening=0.1f;
void Jump()
{
JumpVelocity = 0.5f;
gameObject.rigidbody.useGravity = false;
}
void FixedUpdate()
{
Vector3 pos = transform.position;
if (JumpVelocity != 0)
{
pos.y += JumpVelocity;
JumpVelocity -= JumpDampening;
if (JumpVelocity <= 0)
{
gameObject.rigidbody.useGravity = true;
JumpVelocity = 0;
}
}
transform.position = pos;
}在上述重力作用下,物体在跳起来时是关闭的,在跳弧峰值时又转回来,这样重力就会再次取代而使之下降。在JumVelocity中播放更高的起始值,用于更快的跳跃,在JumpDampening中使用更高的值来减缓跳跃的速度,或者用较低的值来让跳跃变得更高,并且具有更明显的弧度。
或者你可以通过
if(inAir) { // most code has something like this already
Vector3 vel = rigidbody.velocity;
vel.y-=BONUS_GRAV*Time.deltaTime;
rigidbody.velocity=vel;
...
}https://stackoverflow.com/questions/65276674
复制相似问题