Have to change a value in lerp() method but don't know how to do that.

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

So basically I have a kinematic body 3d which moves with a joystick (in the opposite direction of where joystick points, just like a trigger). What i want is to make the kinematic body reflect the direction from which it is coming after hitting the wall (gridmap, in the group “wall”), just like the mirror , when the incident ray collides with the mirror and gets reflected by the mirror with the angle of incidence equal to angle of reflection. I use the area node to know whether the colliding body is in group “walls” or not. Somehow I managed to have the reflection of the velocity, but then it accelerates crazily to the direction of the reflection . Here is the code for my kinematic body:

extends KinematicBody

var acceleration = 10
var topspeed = 11
onready var joystick = get_parent().get_node("Joystick/Joystick_button")
var vel = Vector3()
var speed = 10
var target_dir = Vector2(0, 0)
var a 

 

func _ready():
	pass 
	
func _physics_process(delta):
	#var target_dir = Vector2(0, 0)
	target_dir = -joystick.get_value()
	if joystick.ongoing_drag != -1:
		a = -joystick.get_value()
	if joystick.ongoing_drag == -1 and joystick.i != 0:
		target_dir = a
	

	vel.x = lerp(vel.x, target_dir.x * speed , acceleration * delta)
	vel.z = lerp(vel.z, target_dir.y * speed , acceleration * delta)
	
	
	
	
	vel = move_and_slide(vel, Vector3(0, 1, 0))
	
	
	
	

func _on_Area_body_entered(body):
	
	if body.is_in_group("walls"):
		
		acceleration *= -1

Edit : I have found out that the Vector3.bounce() method works the same way but not able to implement it as it is not bouncing off the walls, like it should do like zig-zag between the walls or like the snooker striker after colliding the edge of the table, it bounces off to the other direction.