Reverse direction when collided

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Akhileswar V

I am trying to create a small platform which left and right.I have an Area2D which detects at both ends of the platform’s range.The platform doesn’t reverse direction after the 5th collisionTwo Raycast2D’sare provided facing x and -x direction.I tried to solve this bug but i couldn’t.I don’t have much experience with Godot.Someone please help.

extends KinematicBody2D

var distance = 0
var velocity = Vector2(10,0)
onready var ray = get_node("RayCast2D")
onready var ray_2 = get_node("RayCast2D2") 


func _ready():
    set_process(true)


func _process(delta):
   while true:
	  distance = distance + self.transform.x.x
	   if ray.is_colliding() || ray_2.is_colliding():
		   velocity.x = -velocity.x
		   print(distance)
	  move_and_collide(-velocity * delta)
	  yield(utils.create_timer(2),"timeout")
 

Could You list the nodes you are using for me.
I don’t understand this statement “I am trying to create a small platform which left and right”

Are you trying to shift the platform right and left? or
Are you trying to get an object to move left and right on the platform?
Is the platform supposed to rotate around center point?

gswashburn | 2018-04-03 21:09