So I have movement controls that I have created, and they do what I want when directly in the script of the child I want to move, but I want to control multiple similar units from their parent script, rather than having script on each child. However, the scrip that I have, which allows for the unit to rotate by 60 degrees and move in the direction that it is facing works only when attached directly to the child. I thought my changes to the code on the parent would have been sufficient but it doesn't seem so.

PROBLEM: So the specific problem is that it will only move left and right with the acceleration and deceleration, which is expected before it turns. However once I rotate my desire would be that it would go in the new rotated direction, however it still only goes left and right. So it moves and rotates, it just doesn't match the direction of its movement to the rotation degree.

Here is the code...

func _process(_delta):
	var i = 0
	var the_unit = arr_ship[i]
	var curr_pos = the_unit.position
	if Input.is_action_just_pressed("accelerate"):
		the_unit.global_position = curr_pos + Vector2(1,0).rotated(deg_to_rad(rotation_degrees)) * Vector2(128,128)
	if Input.is_action_just_pressed("decelerate"):
		the_unit.global_position = curr_pos + Vector2(-1,0).rotated(deg_to_rad(rotation_degrees)) * Vector2(128,128)
	if Input.is_action_just_pressed("rotate_left"):
		the_unit.rotation_degrees -= 60
	if Input.is_action_just_pressed("rotate_right"):
		the_unit.rotation_degrees += 60
  • xyz replied to this.
  • xRegnarokx You're mistakenly taking rotation of the parent instead of rotation of the_unit when rotating the direction vector. Also when multiplying unit vectors with magnitude, it's enough to multiply with a single scalar value instead of a vector containing identical components.

    the_unit.global_position += Vector2(1,0).rotated(deg_to_rad(the_unit.rotation_degrees)) * 128

    If you want to do it more elegantly you can simply use unit's global x basis vector as movement direction. It's a built in vector that points in the current global direction of node's x axis.

    the_unit.global_position += the_unit.global_transform.x * 128

    xRegnarokx You're mistakenly taking rotation of the parent instead of rotation of the_unit when rotating the direction vector. Also when multiplying unit vectors with magnitude, it's enough to multiply with a single scalar value instead of a vector containing identical components.

    the_unit.global_position += Vector2(1,0).rotated(deg_to_rad(the_unit.rotation_degrees)) * 128

    If you want to do it more elegantly you can simply use unit's global x basis vector as movement direction. It's a built in vector that points in the current global direction of node's x axis.

    the_unit.global_position += the_unit.global_transform.x * 128

      xyz wow I feel blind haha, I was assuming that is what was happening I was just looking in the wrong place and not seeing the problem. Thanks so much!

      xyz Also when I use

      the_unit.global_position += the_unit.global_transform.x * 128

      It only moves about 1/3 of what I want it to, however the first one works just fine, not sure what causes that to happen.

      • xyz replied to this.

        xRegnarokx If the node is scaled then you need to normalize the basis vector to make its length exactly 1.0:

        the_unit.global_position += the_unit.global_transform.x.normalized() * 128