I'm trying to create a 2.5D platformer game template where the player's horizontal movement is constrained to a path object. The player should be able to go forward and backward along the curve, and be able to jump onto platforms in the curve's path. (the path can curve left and right to highlight the 3D aspects of the world for the player, but the player shouldn't be able to move off of it)
I'm excited with the result, using a 3d kinematicbody for jumping and to detect collision with the ground, combined with the left and right path movement works well. But when the player runs side-ways into a static-body collision object, it gets pushed off of the path with weird results. Is there a way to make sure the kinematic body stays attached to the path horizontally, but not vertically? Here's my attempt, which just modifies the kinematic demo code slightly:
extends KinematicBody
var g = -9.8
var vel = Vector3()
const MAX_SPEED = 5
const JUMP_SPEED = 5
const ACCEL= 4
const DEACCEL= 8
const MAX_SLOPE_ANGLE = 30
func _ready():
get_parent().set_offset(1)
pass
func _physics_process(delta):
var dir = Vector3() # Where does the player intend to walk to
#get_parent() is getting the pathfollow node and setting the offset of the kinematicbody along the path
if (Input.is_action_pressed("left")):
get_parent().set_offset(get_parent().get_offset() + (5*delta))
set_rotation(Vector3(0,-90,0))
if (Input.is_action_pressed("right")):
get_parent().set_offset(get_parent().get_offset() + ((-5)*delta))
set_rotation(Vector3(0,90,0))
dir.y = 0
dir = dir.normalized()
vel.y += delta*g
var hvel = vel
hvel.y = 0
var target = Vector3(0,dir.y*MAX_SPEED,0)
var accel
if (dir.dot(hvel) > 0):
accel = ACCEL
else:
accel = DEACCEL
hvel = hvel.linear_interpolate(target, accel*delta)
vel = move_and_slide(vel,Vector3(0,1,0))
if (is_on_floor() and Input.is_action_pressed("jump")):
vel.y = JUMP_SPEED