So I am making a Voodoo-type mobile game. To move the cannon, you have to drag across the screen. This is simple, and I have made a system where I can move the coordinates on the x axis of the cannon. This, however, changes the velocity, and slows it down. I have linked a youtube link with the video, as well as putting in my code.
extends Node2D
var ended = false
export(Color) var lineColor = Color(255, 0, 0)
export(float) var lineWidth = 5.0
export(bool) var antialiased = false
var xAxisOnCannonAtStart = 0
var position_start := Vector2.ZERO
var touch_down := false
var position_end := Vector2.ZERO
var difference = 0
func _on_input_event(_viewport, event, _shape_idx) -> void:
if event.is_action_pressed("ui_touch"):
touch_down = true
position_start = event.position
func _input(event) -> void:
if not touch_down:
return
if event is InputEventMouseMotion:
if Input.is_action_pressed("ui_touch"):
if ended == true:
ended = false
else:
ended = false
position_end = event.position
update()
print("position_end = ",event.position.x)
difference = (-(position_start.x - event.position.x))/100
print("The direction of movement is ",difference," in the x axis")
print(self.get_parent().get_node("Cannon").transform.origin)
var zAxisOnCannon = self.get_parent().get_node("Cannon").transform.origin.z
self.get_parent().get_node("Cannon").transform.origin.x = difference+xAxisOnCannonAtStart
func _process(_delta):
update()
func _draw():
if Input.is_action_just_pressed("ui_touch"):
touch_down = true
position_start = get_local_mouse_position()
print("position_start =",get_local_mouse_position().x)
xAxisOnCannonAtStart = self.get_parent().get_node("Cannon").transform.origin.x
if Input.is_action_pressed("ui_touch"):
var mouseLocalPosition = get_local_mouse_position()
draw_line(position_start , mouseLocalPosition, lineColor, lineWidth, antialiased)