Godot Version 4.2
I have a 3D turret that can only rotate in 8 directions (eg. every 45degrees). How can I get it to rotate to the direction corresponding with the β€œzone” that the player is in? Also, how to make turret smoothly rotate between those angles as well.

func _process(delta):
	var node3d_rotation = node3D.global_rotation_degrees.y
	var rotate_target = 0
	node3D.look_at(player.global_transform.origin,Vector3.UP)
	if (node3d_rotation < 25 and node3d_rotation > -25):
		rotate_target = 0
	elif node3d_rotation > 25 and node3d_rotation < 75 :
		rotate_target = PI/4
	elif node3d_rotation > 75 and node3d_rotation < 112 :
		rotate_target = PI/2
	etc...
	turret.rotation.y = rotate_toward(turret.rotation.y, rotate_target, 2 * delta)

Above is my wonky hard coded solution, but I'd like a better way to code this (eg, not depends on global rotation, since I will place/rotate the turret node around the stage). Thanks

Some background on this question
https://forum.godotengine.org/t/gun-turret-aim-at-player-with-only-8-rotating-directions/69080/10

    interesting that you want to rotate only by 45 deg, and then you want to rotate smoothly... so what do you really want? πŸ˜ƒ

    xyz

    how?

    @kuligs2
    both πŸ˜ƒ slow rotate between those 8 angles, depends on player's angle from the turret.

      Gowydot look at that snapped function, it should be possible. There is smooth variant there too.

      @kuligs2
      I read and understand what it does but I don't understand how to implement it.

      • xyz replied to this.

        Finally figured it out using ChatGPT!!
        j/k lol don't kill me I did it myself πŸ˜„ Thank you @xyz

        func _process(delta):
        	turret.rotation.y = rotate_toward(turret.rotation.y, get_8_direction_input(), 2 * delta)
        func get_8_direction_input():
        	var node3d_rotation = node3D.rotation_degrees.y
        	node3D.look_at(player.global_transform.origin,Vector3.UP)
        	var sector = wrapi(snapped(node3d_rotation, 45) / 45, 0, 8)
        	return ([0, PI/4, PI/2, 3*PI/4, PI, -(3*PI/4), -PI/2, -PI/4][sector])

        Some questions:

        1. So you can put var with any values in [] and it will become array [var]?

        2.How does

        return ([0, PI/4, PI/2, 3*PI/4, PI, -(3*PI/4), -PI/2, -PI/4][sector])

        works? like if you put them together like [array1][array2] then it will return the values of [array1] based on values of [array2] ?

        • xyz replied to this.

          Gowydot No, those two sets of array bracket actually have different purpose. The first set of array brackets defines the array object while the second set is the subscript operator that indexes the array. It's equivalent of doing:

          var temp_array = [0, PI/4, PI/2, 3*PI/4, PI, -(3*PI/4), -PI/2, -PI/4]
          return temp_array[sector]

            xyz
            Oh now I see. I'm slow and had to break it down like this to understand πŸ˜†

            var temp_array = ["0", "PI/4", "PI/2", "3*PI/4", "PI", "-(3*PI/4)", "-PI/2", "-PI/4"]
            var sector = 1
            print(temp_array[sector])