Moving Backwards (Automatically)

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

Hey , I am new to programming, and i am “trying” to make a top down game where i can click on the player and make him move, anyway I have a Raycast2d and i want the player to move backwards (whatever the player’s direction is (btw i am using look_at()) when ever RayCast collides with something,Automatically btw, using lerp or something like that…

if $RAYCAST.is_colliding():
     lerp(global_position,....)

(Remember i am a complete Beginner I just started)

Not sure if I can follow… You want to make the player character clickable and make it move backwards (i.e. invert it’s movement direction?) once it has been clicked. And you want to move the character (Is it a KinematicBody2D? A RigidBody2D? Something else?) using linear interpolation (that’s what lerp stands for) as long as the RayCast2D attached to this character (Pointing in which direction? Being how long?) is colliding with anything (Or something particular?). Now where is that automatic, if it’s triggered by the player clicking on the character? And what exactly is your question here?

njamster | 2020-05-11 13:46

Sorry for the delay, alot to do and no time left , Ye I want and I already made (copied the code ofc from gdquest (starpath) https://www.youtube.com/watch?v=Ad6Us73smNs , as you may be thinking I did not Understand most of the code if not all of it), I can click on him (KinematicBody2D) and move him where ever I want (depends on the mape size and i want the player to dodge or … move backward when the raycast collides with the enemy (with a speed lower than the enemy ofc) as long as the player is facing the enemy and as long as raycast2d is colliding (and after i activate it using a button but anyway), wow after typing this this seems complicated than i thought, ye and all that by himself , i want the player to do this, ik this is too far for a beginner but … and also I am making another game at the same time and it’s a strategy game 2d where you can summon your own troops and watch them fight against the Enemy (the Enemy is just the bot… for now) but i have a problem, lets say I have a range troop that has area as range for his arrow, he’s doing fine but when there is more than 1 enemy in the range area or when he reaches the base of the enemy he detect the first enemy and then act like there is no other bodies in his range, i know what the problem is, I use a boolean Body_entered and Body_exited, when the body exited the boolean change to false but at the same time there is a body in the area but the boolean is already set to false because of the last body that exited the area… what the solution in your opinion?
Thanks

lowy98 | 2020-05-13 07:41

:bust_in_silhouette: Reply From: njamster

copied the code ofc from gdquest

Ok, this is far from obvious (as you seem to imply by using “ofc”) and some valuable information that should’ve been part of the question from the outset!

I did not Understand most of the code if not all of it

Then that is what you should work on first! No point in trying to run a marathon, before you learned how to walk. Nobody will teach you the former first!

ik this is too far for a beginner but…

Change the “but” into a period: this is too far for a beginner! To be clear: I don’t think this is complicated at all. What makes it complicated is the fact that you choose to copy a codebase that you do not understand and the fact that you claim to have no former experience with Godot or programming in general.

The answer to your question is: keep track of the direction your character is moving, multiply that direction with a factor of 1 by default and by -0.5 (halved as you want to back up slower than the enemy, inverted as you’re moving backwards) as long as the raycast is colliding with an enemy. Of course things become more complicated when you’re using a pathfinding-algorithm on top of it, but there is no way to answer that in a general and concise manner here given the information you provided.

and also i making another game at the same time

Completely different topic. Create a new question for that as recommended here:

If you have several questions, open several topics. Do not ask several unrelated questions in the same thread, as it will be harder to give you a proper answer for each point.

That being said, the solution is to use an array to keep track of all bodies inside the area individually. Check out my answer over here.

Do you have any social media (like reddit or anything else…) where i can ask you bunch of questions, it seems that know bunch of things that I want to learn them, so what do you think…
back to the topic, when you copy something that is hard and you dont understand, it’s gonna be hard to keep track of the player’s direction, since I dont know how it works in this specific code.
for the second question, hmm it seems like its working (so far) Thanks,

lowy98 | 2020-05-14 18:22