@Gowydot said:
Thanks all for answering. I would like to explain a little clearer on what I aim to do.
This Pathfollow setup would be like a street traffic system ("Trains" are cars). A street light would enable an Area collision which when the car hit it will slowly come to stop. A Timer will then disable this Area collision and cars would move again. (Each car would also be able to detect eachother <= but I haven't got around to try this part yet)
1) This is a traffic light stop part. I've tried MOVE_SPEED *= 0.98 (and lerp) but result is just slow down but not to a stop. I could only changed to =0 to make it completely stop
Maybe try something like this:
MOVE_SPEED *= 0.95
if (MOVE_SPEED <= 0.1):
MOVE_SPEED = 0
MOVE_SPEED
will need to be a variable (var
) and not a constant (const
) for it to work. I don't think Godot will let you modify a constant, but it just occurred to me that it could be contributing to the issue.
2) This code adjustment works and I got multiple cars to run on same path. But then when one car hit the traffic Area all of them would stop (even though I tried separate the script / connect signal to each of them)
Overall, now I am not sure if using Area to signal change to car's move_speed is the way to go?
Hmm, strange. Is the signal connected to all of them? It could be that the signal is being emitted and sent to all the trains, rather than just the one that collided. A simple way to fix this is to pass the train that collided as well and then check it in a condition. Something like this, for example:
# =========
# on the area
signal on_car_stop(car_to_stop)
func on_body_enter(other):
emit_signal("on_car_stop", other)
# =========
# =========
# On the car
# (the function connected to the signal)
func on_car_stop(car_to_stop):
if car_to_stop != self:
return
else:
print ("Stop the car!")
# =========
The other way to handle it, which might be slightly more efficient, is to have the area call a function on the car when it collides. Something like this:
# =========
# on the area
func on_body_enter(other):
if other.has_method("on_car_stop"):
other.on_car_stop()
# =========
# =========
# On the car
func on_car_stop():
print ("Stop the car!")
# =========
3) Part of his "StationEnterance" code is not showing, I'm trying to figure it out. (at StationEnterance's emit_signal part) =also pls ignore the wrong Entrance spelling lol=
What I might try doing is calling a function directly instead of emitting a signal, since you have a reference to the train/car you want to change. Something like this, for example:
# =========
# Rail (no changes needed, I think)
func _on_changeTrainRail(rail,train):
var newRail:Path2D = get_node("Path_"+String(rail))
var newOffset = newRail.curve.get_closet_offset(train.get_node("KinematicBody2D").global.position-newRail.global_position)
train.get_parent().remove_child(train)
newRail.add_child(train)
train.offset = newOffset
# =========
# =========
# StationEnterance
func _on_StationEnterance_body_entered(body):
if !body.get_parent().atStation:
if randf()>0.5:
body.get_parent().atStation = true
# (I'm assuming the body.get_parent() is the rail. If not, you will need a reference to the rail and then can replace "body" with the rail reference)
body.get_parent().__on_changeTrainRail(STATION_ID, body.get_parent())
print("arriving at station")
else:
print("ignoring station")
# =========
I'm not totally sure it will work though, but that is what I would try.