If you put that code on a KinematicBody
it will give you the following error:
Parser Error: The function signature doesn't match the parent. Parent signature is: "Vector3 move_and_slide(Vector3, Vector3=default, bool, int, float, bool)".
That is because KinematicBody
defines a move_and_slide
. And no, you can't have an overload. GDScript does not support that. Instead creating a method with the same name is taken as an attempt to override it, which is failing when it checks if the parameters match. The method move_and_slide
is not virtual anyway.
I also want to mention that the slide
part of the name of move_and_slide
refers to how it interacts with ramps. That is, if the KinematicBody
collides with a surface that it considers a ramp, it will slide on it.
Now, you could put the code directly on _physics_process
, like this:
func _physics_process(delta):
position.x += delta * velocity
Notice I renamed the parameter from _delta
to delta
. You were passing delta
into your move_and_slide
method, but the parameter name was _delta
.
Except - wait - there is no position
in KinematicBody
(there is in KinematicBody2D
), if you want to change the position you can write the origin of the transform:
func _physics_process(delta):
transform.origin.x += delta * velocity
And you need to be aware that that is a teleport. It would not be checking for collisions. Use move_and_slide
(or if you don't want the slide
part, use move_and_collide
) that comes defined in the KinematicBody
class. That way, it does not only moves the KinematicBody
but also takes into account collisions.
By the way, I would argue the variable is a speed (scalar) not a velocity (vector). Yet, I'm keeping the way you named it.
Given that you have unused KinematicBodyWidth
and KinematicBodyHeight
I suspect you don't want to use Godot physic objects. If that is the case, you can use a regular Spatial node, and use physics queries to detect collisions.
I have an explanation from the basics of setting up Godot physics to how to use physic queries elsewhere. Either if you want to use Godot physic objects and don't know how, or if you don't want to use them and use physics queries instead, I think that would help you.
The next errors are that setupKinematicBody
and positionLeftCenter
are not defined. Either define the methods, or remove the calls. I can't help further with those.
By the way, are you sure you want to use _enter_tree
instead of _ready
? And are you sure you don't want to initialize velocity with the value you want (var velocity: float = 100.0
), or you can export it so it is easy to set from the inspector panel (export var velocity: float = 100.0
).
Your code could be just this:
extends KinematicBody
export var speed := 100.0
func _physics_process(delta):
transform.origin.x += delta * speed
Or this:
extends KinematicBody
export var velocity := Vector3(100.0, 0.0, 0.0)
func _physics_process(_delta):
move_and_slide(velocity)