每当运动学 Body 3d (godot) 与网格图、静态 body 或其他运动学 body 发生碰撞时如何反弹?
How to bounce Kinematic Body 3d (godot) whenever it collides with a gridmap, static body or another kinematic body?
我有一个运动学 body,它使用虚拟操纵杆移动。我想要的是我的运动学 body 应该反弹(就像空气曲棍球中的球撞到墙壁或前锋时所做的那样)。我的网格图在一个名为“walls”的组中。这是我的播放器代码:
extends KinematicBody
var acceleration = 10
var topspeed = 40
onready var joystick = get_parent().get_node("Joystick/Joystick_button")
var vel = Vector3()
var speed = 10
var target_dir = Vector2(0, 0)
var a
var b
func _ready():
pass
func _physics_process(delta):
#var target_dir = Vector2(0, 0)
target_dir = -joystick.get_value()
if joystick.ongoing_drag != -1:
a = -joystick.get_value()
if joystick.ongoing_drag == -1 and joystick.i != 0:
target_dir = a
vel.x = lerp(vel.x, target_dir.x * speed , acceleration * delta)
vel.z = lerp(vel.z, target_dir.y * speed , acceleration * delta)
#vel = move_and_slide(vel, Vector3(0, 1, 0))
var collision = move_and_collide(vel * delta)
if collision:
vel = vel.bounce(collision.normal)
编辑:最后使用的 vel.bounce() 不满足要求,因为它 returns 反弹非常低,但我希望它在墙壁之间呈锯齿形反弹,直到我改变方向用我的操纵杆。或者换句话说,我希望我的 Kinematic body 的运动与球在 Flaming core Game 中的运动完全一样(单击 link 以查看其游戏玩法)就像球在之后的反弹方式撞到墙壁或敌人。
尝试更改这些行:
vel.x = lerp(vel.x, target_dir.x * speed , acceleration * delta)
vel.z = lerp(vel.z, target_dir.y * speed , acceleration * delta)
这些:
vel.x += target_dir.x * acceleration * delta
vel.x = min(vel.x, topspeed)
vel.z += target_dir.y * acceleration * delta
vel.z = min(vel.z, topspeed)
并根据需要调整 acceleration
。
我有一个运动学 body,它使用虚拟操纵杆移动。我想要的是我的运动学 body 应该反弹(就像空气曲棍球中的球撞到墙壁或前锋时所做的那样)。我的网格图在一个名为“walls”的组中。这是我的播放器代码:
extends KinematicBody
var acceleration = 10
var topspeed = 40
onready var joystick = get_parent().get_node("Joystick/Joystick_button")
var vel = Vector3()
var speed = 10
var target_dir = Vector2(0, 0)
var a
var b
func _ready():
pass
func _physics_process(delta):
#var target_dir = Vector2(0, 0)
target_dir = -joystick.get_value()
if joystick.ongoing_drag != -1:
a = -joystick.get_value()
if joystick.ongoing_drag == -1 and joystick.i != 0:
target_dir = a
vel.x = lerp(vel.x, target_dir.x * speed , acceleration * delta)
vel.z = lerp(vel.z, target_dir.y * speed , acceleration * delta)
#vel = move_and_slide(vel, Vector3(0, 1, 0))
var collision = move_and_collide(vel * delta)
if collision:
vel = vel.bounce(collision.normal)
编辑:最后使用的 vel.bounce() 不满足要求,因为它 returns 反弹非常低,但我希望它在墙壁之间呈锯齿形反弹,直到我改变方向用我的操纵杆。或者换句话说,我希望我的 Kinematic body 的运动与球在 Flaming core Game 中的运动完全一样(单击 link 以查看其游戏玩法)就像球在之后的反弹方式撞到墙壁或敌人。
尝试更改这些行:
vel.x = lerp(vel.x, target_dir.x * speed , acceleration * delta)
vel.z = lerp(vel.z, target_dir.y * speed , acceleration * delta)
这些:
vel.x += target_dir.x * acceleration * delta
vel.x = min(vel.x, topspeed)
vel.z += target_dir.y * acceleration * delta
vel.z = min(vel.z, topspeed)
并根据需要调整 acceleration
。