I have a problem with movement

Godot Version

4.3

Question

so I have this peice of code and it doesn`t work can someone please help?
extends CharacterBody2D
var picked = get(“metadata/picked”)
var empire = get(“metadata/empire”)
var t_p = Vector2()
var MoveSpeed = 100
var Move_mode = true
var sprite = Image.load_from_file(“res://newmap.png”)

func _ready():
t_p = position
if empire == null:
empire = 0
if picked == null:
picked = false

func _process(delta):
if Input.is_action_just_pressed(“left_click”):
var t_g = get_global_mouse_position()
if picked == true:
if Move_mode == true:
t_p = get_global_mouse_position()
else:
picked = false
else:
if position.x - sprite.get_width()/2 < get_global_mouse_position().x and get_global_mouse_position().x < position.x + sprite.get_width()/2:
if position.y - sprite.get_height()/2 < get_global_mouse_position().y and get_global_mouse_position().y < position.y + sprite.get_height()/2:
picked = true
Move(delta)
func Move(delta):
var t_p_n = (t_p-position)
var t_p_n_y
var t_p_n_x
var t_p_n_y_m
var t_p_n_x_m
for i in range(0,int(MoveSpeed*delta)):
t_p_n_x = t_p_n.x/(abs(t_p_n.x)+abs(t_p_n.y))
t_p_n_y = t_p_n.y/(abs(t_p_n.x)+abs(t_p_n.y))
if t_p_n_x>=0.5:
t_p_n_x_m = 1
if t_p_n_x<0.5 and t_p_n_x>-0.5:
t_p_n_x_m = 0
else:
t_p_n_x_m = -1
if t_p_n_y>=0.5:
t_p_n_y_m = 1
if t_p_n_y<0.5 and t_p_n_y>-0.5:
t_p_n_y_m = 0
else:
t_p_n_y_m = -1
if sprite.get_pixel(position.x+t_p_n_x_m+3000,position.y+t_p_n_y_m+1500) != Color(153,217,234):
move_local_x(t_p_n_x_m,1)
move_local_y(t_p_n_y_m,1)

I know that I can use velocity and stuff, but I need to check the map which I have only a png of, so include that when you give advice.

Can you explain your problem with more details and format your code with a code block

```
# your code
With indentations!
```

Becomes

# your code 
    With indentations!
1 Like

So basicaly there are two problems firstly it doesn’t move when I use move local x and doesn’t even go to move_local_y and here is the proper version

extends CharacterBody2D
var picked = get("metadata/picked")
var empire = get("metadata/empire")
var t_p = Vector2()
var MoveSpeed = 100
var Move_mode = true
var sprite = Image.load_from_file("res://newmap.png")

func _ready():
	t_p = position
	if empire == null:
		empire = 0
	if picked == null:
		picked = false
	
func  _process(delta):
	if Input.is_action_just_pressed("left_click"):
		var t_g = get_global_mouse_position()
		if picked == true:
			if Move_mode == true:
				t_p = get_global_mouse_position()
			else:
				picked = false
		else:
			if position.x - sprite.get_width()/2 < get_global_mouse_position().x and get_global_mouse_position().x < position.x + sprite.get_width()/2:
				if position.y - sprite.get_height()/2 < get_global_mouse_position().y and get_global_mouse_position().y < position.y + sprite.get_height()/2:
					picked = true
	Move(delta)
func Move(delta):
	var t_p_n = (t_p-position)
	var t_p_n_y
	var t_p_n_x
	var t_p_n_y_m
	var t_p_n_x_m
	for i in range(0,int(MoveSpeed*delta)):
		t_p_n_x = t_p_n.x/(abs(t_p_n.x)+abs(t_p_n.y))
		t_p_n_y = t_p_n.y/(abs(t_p_n.x)+abs(t_p_n.y))
		if t_p_n_x>=0.5:
			t_p_n_x_m = 1
		if   t_p_n_x<0.5 and  t_p_n_x>-0.5:
			t_p_n_x_m = 0
		else:
			t_p_n_x_m = -1
		if t_p_n_y>=0.5:
			t_p_n_y_m = 1
		if   t_p_n_y<0.5 and  t_p_n_y>-0.5:
			t_p_n_y_m = 0
		else:
			t_p_n_y_m = -1
		if sprite.get_pixel(position.x+t_p_n_x_m+3000,position.y+t_p_n_y_m+1500) != Color(153,217,234):
			move_local_x(t_p_n_x_m,1)
			move_local_y(t_p_n_y_m,1)

Thanks

Does this ever evaluate to true?

Move local functions are a little strange. As they take the basis/scale of the node2d in question. And since you pass 1 (which equals true), it tries to scale the movement. So if your node2d is scaled in anyway it will scale the movement.

I typically would do this for movement.

global_position.x += t_p_n_x_m
global_position.y += t_p_n_y_m

I would also change this to global position too

func _ready():
	t_p = global_position

And also change every position to global_position.

Yes that evaluates to true as I checked with stpes into. and I will try it.
Thank you

Well I tried, fixed a few bugs but it’s still broken I’ll send the changes

extends CharacterBody2D
var picked = get("metadata/picked")
var empire = get("metadata/empire")
var t_p = Vector2()
var MoveSpeed = 100
var Move_mode = true
var sprite = Image.load_from_file("res://newmap.png")

func _ready():
	t_p = global_position
	if empire == null:
		empire = 0
	if picked == null:
		picked = false
	
func  _process(delta):
	if Input.is_action_just_pressed("left_click"):
		var t_g = get_global_mouse_position()
		if picked == true:
			if Move_mode == true:
				t_p = get_global_mouse_position()
			else:
				picked = false
		else:
			if position.x - sprite.get_width()/2 < get_global_mouse_position().x and get_global_mouse_position().x < position.x + sprite.get_width()/2:
				if position.y - sprite.get_height()/2 < get_global_mouse_position().y and get_global_mouse_position().y < position.y + sprite.get_height()/2:
					picked = true
	Move(delta)
func Move(delta):
	var t_p_n = (t_p-position)
	var t_p_n_y
	var t_p_n_x
	var t_p_n_y_m
	var t_p_n_x_m
	for i in range(0,int(MoveSpeed*delta)):
		t_p_n = (t_p-global_position)
		if abs(t_p_n.x)+abs(t_p_n.y) != 0:
			t_p_n_x = t_p_n.x/(abs(t_p_n.x)+abs(t_p_n.y))
			t_p_n_y = t_p_n.y/(abs(t_p_n.x)+abs(t_p_n.y))
		else:
			t_p_n_x = 0
			t_p_n_y = 0
		if t_p_n_x>=0.5:
			t_p_n_x_m = 1
		elif   t_p_n_x<0.5 and  t_p_n_x>-0.5:
			t_p_n_x_m = 0
		elif t_p_n_x <= -0.5:
			t_p_n_x_m = -1
		if t_p_n_y>=0.5:
			t_p_n_y_m = 1
		elif   t_p_n_y<0.5 and  t_p_n_y>-0.5:
			t_p_n_y_m = 0
		elif t_p_n_y <= -0.5:
			t_p_n_y_m = -1
		if sprite.get_pixel(global_position.x+t_p_n_x_m+3000,global_position.y+t_p_n_y_m+1500) != Color(153,217,234):
			global_position.x += t_p_n_x_m
			global_position.y += t_p_n_y_m

You missed some, change all positions to global_position.

I did some local debugging and it began to work after updating to all global_position

I don’t know what I did wrong but it’s still isn’t working for me

I have found the problem. I messed up in taking the wrong sprite fo identifying if I am trying to pick the unit to move it

It didn’t help but I have found that for some reason it t_p_n is always equal to zero

I was able to solve the t_p_n issue but it just isn’t moving

I tried to change some things back, but I had to change some things locally like the get_meta() calls.

extends CharacterBody2D
var picked = false
var empire = 0
var t_p = Vector2()
var MoveSpeed = 100
var Move_mode = true
var sprite = Image.load_from_file(\"res://icon.svg\")

func _ready():
	t_p = global_position
	if empire == null:
		empire = 0
	if picked == null:
		picked = false
	
func  _process(delta):
	if Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT):
		var t_g = get_global_mouse_position()
		if picked == true:
			if Move_mode == true:
				t_p = get_global_mouse_position()
			else:
				picked = false
		else:
			var width = sprite.get_width()/2
			var height = sprite.get_height()/2
			var g_mouse : Vector2 = get_global_mouse_position()
			var x_bool : bool = global_position.x - width < g_mouse.x and g_mouse.x < global_position.x + width
			var y_bool : bool = global_position.y - height < g_mouse.y and g_mouse.y < global_position.y + height
			if x_bool:
				if y_bool:
					picked = true
	Move(delta)
func Move(delta):
	var t_p_n = (t_p-global_position)
	var t_p_n_y
	var t_p_n_x
	var t_p_n_y_m
	var t_p_n_x_m
	for i in range(0,int(MoveSpeed*delta)):
		t_p_n = (t_p-global_position)
		if abs(t_p_n.x)+abs(t_p_n.y) != 0:
			t_p_n_x = t_p_n.x/(abs(t_p_n.x)+abs(t_p_n.y))
			t_p_n_y = t_p_n.y/(abs(t_p_n.x)+abs(t_p_n.y))
		else:
			t_p_n_x = 0
			t_p_n_y = 0
		if t_p_n_x>=0.5:
			t_p_n_x_m = 1
		elif   t_p_n_x<0.5 and  t_p_n_x>-0.5:
			t_p_n_x_m = 0
		elif t_p_n_x <= -0.5:
			t_p_n_x_m = -1
		if t_p_n_y>=0.5:
			t_p_n_y_m = 1
		elif   t_p_n_y<0.5 and  t_p_n_y>-0.5:
			t_p_n_y_m = 0
		elif t_p_n_y <= -0.5:
			t_p_n_y_m = -1
		if sprite.get_pixel(global_position.x+t_p_n_x_m,global_position.y+t_p_n_y_m) != Color(153,217,234):
			global_position.x += t_p_n_x_m
			global_position.y += t_p_n_y_m

1 Like

I will try debugging it because something is a but wrong. Still thanks for help

The strangest problem is that my characterbody2d doesn’t change it’s position, I will check a but more

Found the problem. the sprite isn’t folowing the character body

for some reason I just can’t make the sprite follow the body

I was able to fix it

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