• 3D
  • Help changing speed while following a path

So I've been playing around with a racing game in which the AI follows a predetermined path3D. I figured out how to create a basic path and get my kinematic body to follow it at one consistent speed, but now I'd like to figure out how to use GDscript to change the speed throughout.

I'd really like to play with each NPC's speed as they travel along the path. It seems like offset or unit offset should be able to achieve this quite easily, but I'm not sure how I'd put that into code or where. I'm assuming under the path follow node? For example, my current path is about 550 units right now. What if I want to change the speed from 5 to 5.5 at 100 units? What if I want to be able to change the speed at every 100 unit variable?

Currently my code is set up under the kinematic body which is the child of a path follow node which is the child of a path node. I got this code from watching the starfoxish youtube tutorial and getting rid of all the gun elements.

extends KinematicBody

var parent
var speed = 5


func _ready():
parent = get\_parent()

func _physics_process(delta):
parent.offset += delta \* speed

Any help would be greatly appreciated!

I managed to figure out a simple way to change the speed througout.

extends KinematicBody

var parent
var speed = 5


func _ready():
	parent = get_parent()

func _physics_process(delta):
	parent.offset += delta * speed
	if parent.offset > 0: speed = 5
	if parent.offset > 10: speed = 4
	if parent.offset > 30: speed = 5
	if parent.offset > 60: speed = 6

Now though, I want to assign each section variable speeds that have equal odds of being assigned. So for points 10-30 which are currently at speed 4, how would I get the code to randomly choose being the speed being 4 or 5? What about 4, 4.5, or 5?

Thanks

6 days later

Hi,

I'm (trying to) make a racing game aswell. How is your setup going? Mine went fine on a flat surface, but today I added a "rough" track made in Blender (together with collision mesh), but isn't able to even get up a slight hill for some reason.

Pretty good, pathfollow is a fantastic tool once you get the hang of it. Have you looked into

Should shed some light on gravity as it applies to hills. Unfortunately I’m unfamiliar with anything blender. Good luck