0 votes

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.

in Engine by (44 points)

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.