I downloaded the project and messed around with the project until I found a solution. The issue seemed to be how the Vector2 (posDiff
) passed to the launched
function was setup. I rewrote the code so it hopefully accomplishes what you are looking to achieve. From my testing, it now works with any position and responds like the first jump in the GIF, but for all jumps.
Here is the code:
extends Area2D
export var mass = 5.0
const speed = 5
var shoot = false
var detect = false
var velocity = Vector2(0, 0)
var mouseMotion = false
var ready = false
onready var imageSpr = $Sprite
func _process(delta):
if shoot:
velocity += gravity_vec*gravity*mass*delta
global_position += velocity*delta
if not mouseMotion:
global_rotation = velocity.angle()
pass;
func _input(event):
if Input.is_action_just_released("BUTTON_LEFT") and ready:
imageSpr.set_flip_h(false)
imageSpr.set_flip_v(false)
mouseMotion = false
# Create a vector2 pointing from the mouse to this object.
var vector_from_mouse_to_global_position = self.global_position - get_global_mouse_position();
# (optional) Multiply by 0.5 to reduce the speed.
vector_from_mouse_to_global_position *= 0.25;
# Launch the RigidBody!
launched(vector_from_mouse_to_global_position*speed)
detect = true
if event is InputEventMouseMotion and Input.is_mouse_button_pressed(BUTTON_LEFT):
imageSpr.set_flip_h(true)
imageSpr.set_flip_v(true)
mouseMotion = true
# Use look_at to calculate the angle.
# The reason we want to use look_at is because angle_to gets the shortest
# angle difference, which may not always be the visually correct angle.
look_at(get_global_mouse_position());
if Input.is_action_just_pressed("ui_end"):
get_tree().reload_current_scene()
func launched(initial_velocity : Vector2):
shoot = true
velocity = initial_velocity
func _on_Ball_body_entered(body):
if body.collides == true and detect:
velocity = Vector2(0, 0)
shoot = false
detect = false
func _on_Trail_launchedSet():
ready = true
I also took the liberty of making a few changes to the trail so that it more closely follows the mouse:
extends Line2D
export(NodePath) var targetPath
var target
var point
var launchReady = false
var signalNotEmit = false
var numberOfPoints = 0
signal launchedSet
func _ready():
target = get_node(targetPath)
global_position = Vector2(0, 0)
global_rotation = 0
point = target.global_position
add_point(point)
func _process(delta):
numberOfPoints = get_point_count()
global_position = Vector2(0, 0)
global_rotation = 0
point = target.global_position
set_point_position(0, point)
if launchReady == true and signalNotEmit == false:
signalNotEmit = true
emit_signal("launchedSet")
func _input(event):
if event is InputEventMouseMotion and Input.is_mouse_button_pressed(BUTTON_LEFT):
if numberOfPoints == 1:
# Points added to a Line2D are expected to be local, so we want
# to get the local mouse position.
add_point(get_local_mouse_position())
launchReady = true
signalNotEmit = false
else:
set_point_position(1, get_local_mouse_position())
launchReady = true
elif not Input.is_mouse_button_pressed(BUTTON_LEFT):
if numberOfPoints == 2:
remove_point(1)
launchReady = false
Hopefully this helps! :smile: