Negative velocity makes instantiated object just stay in place

Godot Version

4.7.2

Question

ive tried to give an instantiated object random velocity and it works fine if it the velocity is positive but when velocity is negative ( multiplied by *=1 ) it just stays in place instead

		for i in range(1):
			randomize()
			var InstancePR = FR.instantiate()
			var Velocity1 = randi_range(20,300)
			var way = randi_range(1,2)
			InstancePR.velocity.x = Velocity1
			print("#########")
			if way == 2:
				InstancePR.velocity.x *= -1
				print('was reversed')
			else:
				print("wasnt reversed")
			print("InstancePR.velocity.x : ",InstancePR.velocity.x)
			print("Velocity1 : ",Velocity1)
			print("way : ",way)
			add_child(InstancePR)

when it should’ve been reversed
the console output :
was reversed
InstancePR.velocity.x : -125.0
Velocity1 : 125
way : 2

but just stayed in pace
( im sorry for the post being a bit cut off i acidently clicked publish before it was done and you cant edit pending ones )

Try this:

var InstancePR = FR.instantiate()
InstancePR.global_position = global_position # Give it a starting position

var Velocity1 = randi_range(20, 300)
var way = randi_range(1, 2)

if way == 2:
    InstancePR.velocity.x = -Velocity1 # Directly assign the negative value
else:
    InstancePR.velocity.x = Velocity1

add_child(InstancePR)

so changed code to this

		for i in range(1):
			randomize()
			var InstancePR = FR.instantiate()
			var Velocity1 = randi_range(20,300)
			var way = randi_range(1,2)
			print("#########")
			if way == 2:
				InstancePR.velocity.x = -Velocity1
				print('was reversed')
			else:
				InstancePR.velocity.x = Velocity1
				print("wasnt reversed")
			print("InstancePR.velocity.x : ",InstancePR.velocity.x)
			print("Velocity1 : ",Velocity1)
			print("way : ",way)
			add_child(InstancePR)

( without all the prints [ i think its easier to read ] )

		for i in range(1):
			randomize()
			var InstancePR = FR.instantiate()
			var Velocity1 = randi_range(20,300)
			var way = randi_range(1,2)
			if way == 2:
				InstancePR.velocity.x = -Velocity1
			else:
				InstancePR.velocity.x = Velocity1
			add_child(InstancePR)

i removed to
InstancePR.global_position = global_position
since it made it instantiate somewhere else and not at the parent’s location
it still went right as it should

[ image with a fragment of rock a bit right i cant have 2 atachments ]

#########
wasnt reversed
InstancePR.velocity.x : 147.0
Velocity1 : 147
way : 1

but stayed in the middle when it was supposed to go left

image
#########
was reversed
InstancePR.velocity.x : -125.0
Velocity1 : 125
way : 2

so the way it was reversed wasnt the issue

Setting global position if the node is not in the scene tree makes no sense and will not behave as expected. Always do it after you call add_child(). In this case It’d be best to call add_child() immediately after the instantiation.

changed to call add_child immediately after the instantiation also added back the
InstancePR.global_position = global_position
line after the add_child() it does not break it this time but still dosent fix it or do anything really issue also stayed still

Post the code you now have.

	for i in range(10):
		randomize()
		var InstancePR = FR.instantiate()
		add_child(InstancePR)
		InstancePR.global_position = global_position
		var Velocity1 = randi_range(20,300)
		var way = randi_range(1,2)
		if way == 2:
			InstancePR.velocity.x = -Velocity1
			print('was reversed')
		else:
			InstancePR.velocity.x = Velocity1
			print("wasnt reversed")
		print("InstancePR.velocity.x : ",InstancePR.velocity.x)
		print("Velocity1 : ",Velocity1)
		print("way : ",way)

here

the results are the same as before

Post the instance’s code. Print the velocity from its processing function.

ohhh yeah i forgot that might have been the issue
there was a check to stop it if the velocity was to low but velocity.x wasnt abs()
thank you for reminding me about that part