I've done this before, but with Godot 4, I'm having trouble understanding how to use tweens since they are no longer nodes. I'm also not sure what script I should put the code in (Main, Area2D, CharacterBody2D).

Example of tween in Godot 4:

extends Node2D
...
func move_to_new_position() -> void:
        var tween: Tween
        tween = create_tween()
        tween_property(self, "position", Vector2(5.0, 10.0), 2.0)
        await tween.finished

    For slowing gradually to a stop using a tween, look up using easing functions with tweens. You basically want your curve to look like a 🌈 : get near the stop location quickly and then slowly finish the rest of the way.

      DaveTheCoder

      I'll experiment with this, but I'm confused what some of this code means. Like "-> void:" and the values inside Vector2(). Would "position" be the right thing to use? Essentially, I have a variable named "speed" with a value of 100 and I want that variable to gradually decrease to 0.

        award What do you mean by "curve" and "stop location"?

        NJL64 I'm confused what some of this code means

        The "->void" is static typing, which I always use.
        https://docs.godotengine.org/en/4.2/tutorials/scripting/gdscript/static_typing.html#static-typing-in-gdscript

        # Tween speed from 100.0 to 0.0 over two seconds.
        var tween: Tween = create_tween()
        tween_property(self, "speed", 0.0, 2.0).from(100.0)

        https://docs.godotengine.org/en/4.2/classes/class_tween.html#class-tween-method-tween-property
        https://docs.godotengine.org/en/4.2/classes/class_propertytweener.html#class-propertytweener-method-from

          DaveTheCoder

          I'm sorry, but could I have a little more help. I'm still learning and not a total math wiz, so I think I want to change my code so that I'm not dividing because if I do, it means the higher the speed value, the slower the enemy moves. It would be more simple for me to understand if the value worked so that lower is slower and higher is faster.

          This is the code for my enemy (it uses a signal from my laser which is an Area2D).

          Here is the full project so far.

          project.zip
          492B

          Just to reiterate, I want the value of speed to decrease gradually when it enters Area2D (the laser) until it hits 0. I think the tween code I applied worked weird because of the division code.

          That .zip only contains the project.godot file, not the full project folder. But don't include the .godot subfolder. It's usually very big, and is automatically created.

            @"DaveTheCoder"#p130750 How did you comment that out without getting the error: "Identifier "tween" not declared in the current scope."? Why did it need to be commented out?

            The issue I'm having is that I'm not getting the result I want. One of the enemies eventually disappears and the other only slows down because it's already reaching the player.

            I'm trying to make it so that the laser slows down the enemy to a stop before killing it, and I'm a complete novice, so I'm just a little confused how to go about it.

            Like I said in the previous comment, I would like alter my "move toward player" code to use a method other than division so that speed can have normal values "higher number value is faster, lower number value is slower".

            This is because I want to gradually decrease the value of speed until it reaches 0, under the condition that the enemy is inside the laser(Area2D)

            Did you move the tween variable to inside "func _on_laser_body_entered(body)"? or just comment it out entirely with no tween variable? What did you do that made it so you couldn't find any issues when running the project?

              NJL64 Why did it need to be commented out?

              It was accessing the scene tree, but didn't have the @onready annotation. That script doesn't use a tween, so it wasn't needed anyway.

              _on_laser_body_entered() in Main.gd isn't getting called.

              I'll need more time to figure out how the project is supposed to work.

                DaveTheCoder Thank you. I appreciate your time and help. I'll make efforts to better educate myself in the meantime too.


                I changed my enemy code a bit and I got it working the way I wanted for the most part. The enemy slows down to a stop when it enters Area2D(the laser), but it doesn't work on enemies after they've spawned.

                Even if a spawned enemy enters the Area2D, it will affect the other enemy (which was already there, it didn't spawn), but nothing will happen to the enemy that did spawn from the EnemySpawner.

                EnemySpawner script: