#Repeat a Moving Platform Backwards on Demand

10 messages · Page 1 of 1 (latest)

neat brook
#

Hello, I need to write the script for making my moving platform reverse course when it receives the signal from the Area3D and then reset again. The script is on an elevator. I've only found info on platforms that are moving constantly. I want mine to only move when it detects the player. Here is the script:

extends AnimatableBody3D

@export var a := Vector3()
@export var b := Vector3()
@export var time: float = 2.0
@export var pause : float = 0.7

func _on_area_3d_body_entered(body: Node3D) -> void:
if body.is_in_group("Player"): # Replace with function body.
move()

func move():
var move_tween = create_tween()
move_tween.tween_property(self, "position", b, time).set_trans(Tween.TRANS_SINE).set_delay(pause)
await move_tween.finished

Originally, the script included below for endless repeating:

move_tween.tween_property(self, "position", a, time).set_trans(Tween.TRANS_SINE).set_delay(pause)
#await move_tween.finished
#move()

Can anyone help, please? Thanks.

split quiver
#

If you are using a animation player I think you can play the animation in reversed

neat brook
#

Thank you, but I'm using a tween. I have the script to make it repeat automatically, but not on demand. I don't know what to write.

rose pebble
#
@onready var intiial_position = position
@xport var maximum_distance = Vector3()

var destination: Vector3()

var tween: Tween = null

func move()
  if tween != null:
    tween.kill
  tween = create_tween()
  if intial_position == position:
    destionation = maximum_distance
  else:
    destination = initial_position

  var time := 0.0
  tween.tween_property(self, "position", destination, time)
#

and then you call the function when you enter and exit the area

neat brook
rose pebble
neat brook
neat brook
#

It worked with some tweaks! Thank you! Here's what I have for anyone else who would like to do this. I didn't use the exit signal because the elevator would go back to the lower floor when I exit, leaving me stranded. I only used the enter one.

extends AnimatableBody3D

@export var a := Vector3()
@export var b := Vector3()
@export var time: float = 2.0

var destination = Vector3()
var tween: Tween = null

func _on_area_3d_body_entered(body):
if body.is_in_group("Player"):
move()

func move():
if tween != null:
tween.kill()
if a == position:
destination = b
else:
destination = a
tween = create_tween()
tween.tween_property(self, "position", destination, time)

rose pebble
#

ah then i misunderstood what you want to achieve