Switch character control while using same mobile joystick

Godot Version

Godot Engine v4.2.2.stable.official.15073afe3

Question

Good day. I’m making a fishing game. I have a mobile joystick and it works just fine with my character. But. I need to use it when i cast a float (choosing a place to instantiate float in the lake). Character movement disabled after fishing starts with “if fishing” statement. But i don’t get how to connect my joystick to the “float character”. When i try to add same movement code the float - it gives me posVector null error. Any advice please? Here’s the code.

Knob of Joystick:

extends Sprite2D

@onready var parent = $".."
@onready var knob = $"."

var pressing = false

@export var maxLenght = 20
@export var deadZone = 5

func _ready():
	maxLenght *= parent.scale.x 
	#knob.texture = preload("res://Assets/Knob.png")
	

func _process(delta):
	if pressing:
		if get_global_mouse_position().distance_to(parent.global_position) <= maxLenght:
			global_position = get_global_mouse_position()
		else:
			var angle = parent.global_position.angle_to_point(get_global_mouse_position())
			global_position.x = parent.global_position.x + cos(angle)*maxLenght
			global_position.y = parent.global_position.y + sin(angle)*maxLenght
		calculateVector()
	else:
		global_position = lerp(global_position, parent.global_position, delta*50)
		parent.posVector = Vector2(0, 0)
	#check vector
	#print(parent.posVector)
func calculateVector():
	if abs((global_position.x - parent.global_position.x)) >= deadZone:
		parent.posVector.x = (global_position.x - parent.global_position.x)/maxLenght
	if abs((global_position.y - parent.global_position.y)) >= deadZone:
		parent.posVector.y = (global_position.y - parent.global_position.y)/maxLenght

func _on_button_button_down():
	pressing = true

Joystic:

extends Node2D


var posVector: Vector2

Player movement:

func handleInput(x):
	#set movement direstion
	var moveDirection = joystick.posVector
	# applies movement
	if !fishing:
		velocity = moveDirection * SPEED * x
		#moves float if not casted and not in reeling process

func _physics_process(delta):
	handleInput(delta)
	move_and_slide()

Float:

extends CharacterBody2D


const SPEED = 300.0

@onready var joystick = $Camera2D/UI/Joystick


func handleInput(x):
	#set movement direstion
	var moveDirection = joystick.posVector
	# applies movement
	velocity = moveDirection * SPEED * x
		#moves float if not casted and not in reeling proces

func _physics_process(delta):
	handleInput(delta)
	#var direction = Input.get_axis("moveLeft", "moveRight")
	#if direction:
		#velocity.x = direction * SPEED
	#else:
		#velocity.x = move_toward(velocity.x, 0, SPEED)

	move_and_slide()

and error: Invalid get index 'PosVector" (on base: null instance)

Ok. It appears I had a mistake in my path to the “Joystick” node :expressionless:. It’s “. .” not “.” . That’s why I get null values. I managed to make my float move using Joystick with this code:

extends CharacterBody2D


const SPEED = 300.0
var canMoveFloat = true
@onready var joystick = $"../Camera2D/UI/Joystick"

func _physics_process(delta):
	
	if canMoveFloat:
		#var direction = Input.get_axis("ui_left", "ui_right")
		var direction = joystick.posVector
		
		if direction:
			velocity = direction * SPEED * delta

		move_and_slide()

now i wonder why my float keep on moving even when I don’t press any direction :thinking:quite frustrating.

few moments later: It keeps moving because of hierarchy mistake. I should have move “move_and_slide()” under “if direction:”. Because if not - it will keep on moving it the last set direction with the last set speed :sweat_smile:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.