Character Over Path Movement Issue

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Alex Pires

Hi guys!

I’ve spending a lot of time trying to properly make a character walk over a path, so I found a video about this subject using UE4:

https://www.youtube.com/watch?v=VJiTHlypTZQ

I want to make exactly this in Godot!

What I got so far:

  • Follow the path in x-axis
  • Acceleration/Deacceleration, Jump, Fall into the path.

What’s wrong:

1 - The character doesn’t turn along the path, is always facing forward.
2 - The character doesn’t walk in the z-axis, I think it’s because of 1.

So, I made a video:

https://www.youtube.com/watch?v=V5rt6TLiayY

My code:

 extends KinematicBody


var time = 0
var aceleracao = Vector3()
var gravity = Vector3(0,-50,0)
var normal_chao = Vector3(0,1,0)
const atrito = 1.0
const walk_vel = 60
const jump_vel = 20
var pointos=[]
onready var cam1 = get_parent().get_node("Camera")
onready var cam2 = get_parent().get_node("Camera2") 
onready var pathz = get_parent().get_node("Path")
onready var dummy_ref = get_parent().get_node("Path/PathFollow")

var cont=0

func _ready():
	
	#---- GET THE START POINT AND MOVE TO IT --start
	var pts = pathz.curve._data.points
	for p in pts:
		if p!=Vector3(0,0,0):
			pointos.append(p)
	translation = pointos[0]
	#---- GET THE START POINT AND MOVE TO IT --end


func _process(delta):
	
	# change camera on press "enter":
	if Input.is_action_just_pressed("ui_accept"):
		if cam1.current:
			cam2.current=true
		else:
			cam1.current=true
	
	
	# -- for tests only --
	#var next =  pathz.curve.get_closest_point(translation)
	#dummy_ref.offset = pathz.curve.get_closest_offset(translation)
	#dummy_ref.translation = mald.curve.get_closest_point(translation)
	# --------------------
	
	
	# PUT HERO STUCK IN THE PATH
	translation.x = pathz.curve.get_closest_point(translation).x
	translation.z = pathz.curve.get_closest_point(translation).z
	
	#target = pathz.curve.get_closest_point(translation)
	
	var target_vel=0
	dummy_ref.translation.y = translation.y	
	
	aceleracao += delta * gravity

	aceleracao = move_and_slide(Vector3(aceleracao.x,aceleracao.y,aceleracao.z), normal_chao, atrito)

	if Input.is_action_pressed("ui_right"):
		if rotation_degrees.y>0:
			target_vel+=1
		else:
			rotate_y(10*delta)
	if Input.is_action_pressed("ui_left"):
		if rotation_degrees.y<-170:
			target_vel-=1
		else:
			rotate_y(-10*delta)

	if Input.is_action_just_pressed("ui_up"):
		if is_on_floor():
			aceleracao.y = jump_vel
	
	target_vel *= walk_vel
	aceleracao.x = lerp(aceleracao.x, target_vel, 0.1)

I think I’m close to achieve this, but I’m not very good with coding.

Thank you in advance and sorry about my english.


===> Download my project here <===