Them pesky roaches. Never move as you'd expect them to.
But you're in luck. Things are much simpler than it looks at first.
basis.z is a whole vector, although its name may mislead you into thinking it's just the z component of a vector.
So if you want to move something in that direction you simply use that vector as the velocity vector:
velocity = basis.z
move_and_slide(velocity)
However, we need magnitude (length) of the velocity vector to match roach's speed:
velocity = basis.z * speed
move_and_slide(velocity)
But there's one final gotcha there. Although basis.z normally has the length of exactly 1, it may have a different length if the roach is scaled along its z axis. So we should take care of that by normalizing it:
velocity = basis.z.normalized() * speed
move_and_slide(velocity)
There are couple of other things I noticed in your code that could be improved:
Firstly, be consistent with method call syntax. It makes code less confusing. So either always call your methods simply by name: object.my_method(args)
or always use call: object.call("my_method", args)
. Imho the latter should be avoided.
The second thing, a method named move_roach() has no business deciding whether to move the roach or not. It should always move it, as its name suggests. The decision should be made by the caller. Otherwise the method would be better off having a more fitting name like try_move_roach().
And finally if the object is a roach, the word 'roach' should really be excluded from its method and property names, for brevity. It's self evident that all names pertain to a roach as the class itself represents a roach.
So to put all this together:
class_name Roach
extends KinematicBody
var dead = false
var frame_count = 0
const speed = 2.0
func _physics_process(delta):
if not dead:
frame_count += 1
if frame_count < 200:
move()
func move():
move_and_slide($Roach.global_transform.basis.z.normalized() * speed)