0 votes

So basically I have this ball and I want this ball to follow the x position of the mouse but then get shot in a direction after I press left click. But when I do press left click the ball just moves to the position it was at at the very beginning and then gets shot.

extends RigidBody2D

var is_shot := false
var does_object_need_force := true

var velocity := Vector2(200, -200)

# Called when the node enters the scene tree for the first time.
func _ready():
    pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
    if !is_shot:
        if Input.is_action_just_pressed("ui_left_click"):
        is_shot = true

if !is_shot:
    self.position.x = get_global_mouse_position().x
    self.linear_velocity = Vector2.ZERO
elif does_object_need_force:
    self.position.x = self.position.x
    self.linear_velocity = velocity
    does_object_need_force = false

var hit_bodies : = get_colliding_bodies();
for body in hit_bodies:
    if body.is_in_group("Brick"):
        body.queue_free()
    elif body.is_in_group("Lost Wall"):
        get_tree().reload_current_scene()
pass
Godot version v3.4.2
in Engine by (48 points)

1 Answer

0 votes

I don't think this is what is happening. I guess You interpret it wrong, can You show printscreens or short film ?
WHat I see is:
-ball is following mouse at start
-you click, isshot becomes true, elif line kicks in for ONE frame, linear velocity becomes 200,-200 for ONE frame teleporting ball to the top right
-is
shot is true, need_force is false, so neither of conditional line work, RIgidBody is moving in a built-in way till the end

Use apply_force instead of linear velocity when dealing with RigidBody. When mouse is holding your ball get the direction ball is heading ( mouse global position - ball global position ) one line before you set balls position to mouse position. Then, when you release your button, use this direction to apply force to Rigid Body ( direction.normalized() * speed), this way it can actually be done in ONE frame.

by (5,220 points)
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 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 webmaster@godotengine.org with your username.