Looks like you're not logged in

Login or Register to continue

Lesson Q&A

Use this space for questions related to what you're learning. For any other type of support (website, learning platform, payments, etc...) please get in touch using the contact form.

  • Problem with turning on Physics InterpolationCGQQLIf physics interpolation is turned on, I would see an extra bullet sprite flash for a fraction of a second near the player whenever "shoot" is pressed. This happens in the solution project as well. What's going on? And is there a way to fix it? 4 0 Feb. 15, 2025
  • My Solution + Questionmilk_manHere is my solution, I think I prefer using enum instead of a bool variable in this case. Also coied the condition with `aim_direction.length() > 0.1` ```gdscript extends Node2D enum {CONTROLLER, MOUSE} var _input_type := MOUSE func _unhandled_input(event: InputEvent) -> void: if event is InputEventJoypadMotion or event is InputEventJoypadButton: _input_type = CONTROLLER elif event is InputEventMouseMotion or event is InputEventMouseButton or InputEventKey: _input_type = MOUSE func _process(delta: float) -> void: var aim_direction := Vector2.ZERO if _input_type == CONTROLLER: aim_direction = Input.get_vector("aim_left", " aim_right", "aim_up", "aim_down") elif _input_type == MOUSE: aim_direction = global_position.direction_to(get_global_mouse_position()) if aim_direction.length() > 0.1: global_rotation = aim_direction.angle() z_index = 1 if aim_direction.y < 0.0 else 3 ``` Now I got several questions: 1. Why was the following segment written under the Code Reference: `@onready var _weapon_anchor: Marker2D = %WeaponAnchor`. It's not used anywhere. Also a followup question to that, do we even need to create a **WeaponAnchor**(Marker2D)? If I delete it and make the Weapons and Hands children of **WeaponPivot** instead, everything works just find, I can still rotate. 2. my mouse is connected to the pc via cable. Now sometimes, it will move just so very slightly on its own, but its enough to effect the hands rotation while rotating them with the joystick. Is there a way to make godot read the mouse only after a certain amount of motion. 3. I noticed that when using rotation instead of global_rotation like this: ```gdscript if aim_direction.length() > 0.1: -global_rotation = aim_direction.angle() +rotation = aim_direction.angle() ``` the rotation of the hands while using the controller is so much more smooth. If I use global_rotation instead, it's less smooth, more jittery, hard to explain. Just something is off with the feel. Am i being crazy or is there some explanation to this 1. 1 0 Jan. 27, 2025
  • Joypad aiming and Z index: PurpleSunriseJust a thing that I noticed. In the Z index section to make the WeaponPivot to be drawn behind the character I can't see the part where you have to disable the property Z as relative. It took me a while to understand why it was not working! If Z as relative is enabled the node will take the parent's Z index and nothing will ever change. So you need to disable it in order to be able to change the Z index individually. Also I found another way to use the gamepad I don't know if this is also acceptable: ```gdscript extends Node2D var direction : Vector2 = Vector2.RIGHT var last_valid_direction : Vector2 = Vector2.RIGHT var last_input_type : String = "" # Called when the node enters the scene tree for the first time. func _ready() -> void: pass # Replace with function body. # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(_delta: float) -> void: match last_input_type: "mouse": direction = global_position.direction_to(get_global_mouse_position()) if direction != Vector2.ZERO: last_valid_direction = direction rotation = last_valid_direction.angle() "gamepad": direction = Input.get_vector("aim_left","aim_right","aim_up","aim_down") if direction != Vector2.ZERO: last_valid_direction = direction rotation = last_valid_direction.angle() if last_valid_direction.y < 0.0: print(z_index) z_index = 1 else: print(z_index) z_index = 3 func _unhandled_input(event: InputEvent) -> void: if( event is InputEventMouseButton or event is InputEventMouseMotion or event is InputEventKey ): last_input_type = "mouse" elif( event is InputEventJoypadButton or event is InputEventJoypadMotion ): last_input_type = "gamepad" ``` It was fun to solve these little problems! Thank you! 11 0 Jan. 23, 2025
  • Shoot function is called twicejongtWhen I hit the Space bar (shoot action), it will always shoot 2 bullets in two directions. One goes to the right, one to the aiming direction (both with slightly altered angles like expected). I'm not quite sure when I introduced this bug, but it came apparent in this or the next lesson. I tried to find the issue myself and also tried to alter the code to be more like yours, but I can't seem to solve the issue. In the remote view, I can see two objects being added as children of the main scene, one called "Bullet" and a similar one called "@area2D@2". I start wondering if it's not a problem in my code but rather something about my hardware or my Input settings, or some other Godot setting? Here are my scripts that are most likely relevant for this: weapon.gd ```gdscript extends Node2D @export var WeaponSprite :Sprite2D = null @export var SpawnedBullet:PackedScene = null @export var max_range := 1000.0 @export var BulletSpeed := 750.0 @export var audio_stream_player: AudioStreamPlayer2D = null @export_range(0.0, 360.0, 1.0, "radians_as_degrees") var random_angle := PI / 12.0 func _physics_process(_delta: float) -> void: if Input.is_action_just_pressed("shoot"): shoot() func shoot(): var spawn: Node = SpawnedBullet.instantiate() get_tree().current_scene.add_child(spawn) spawn.global_position = global_position spawn.global_rotation = global_rotation spawn.rotation += randf_range(-random_angle,random_angle) spawn.max_range = max_range spawn.speed = BulletSpeed audio_stream_player.play() ``` weapon_pivot.gd ```gdscript extends Node2D var uses_gamepad := false func _unhandled_input(event: InputEvent) -> void: if event is InputEventMouseMotion or event is InputEventKey: uses_gamepad = false elif event is InputEventJoypadButton or event is InputEventJoypadMotion: uses_gamepad = true func _process(_delta: float) -> void: var MousePos = get_global_mouse_position() var direction := Vector2.ZERO if uses_gamepad == false: direction = global_position.direction_to(MousePos) else: direction = Input.get_vector("aim_left","aim_right","aim_down","aim_up") if direction.length() > 0.1: rotation = direction.angle() z_index = 3 if direction.y < 0.0: z_index = 1 ``` 6 0 Jan. 17, 2025
  • Invisible BulletsPixAngelWhen i'm using shoot key, button or gamepadbutton, my bullets are invisible although they appear in my remote panel wether on player.tscn or my test_room.tscn I checked how i instantiate in my weapon.gd: ```gdscript func shoot() -> void : var new_bullet = bullet.instantiate() get_tree().current_scene.add_child(new_bullet) ``` When I open my weapon.tscn, it works very well but not the others. I modified, top level option, z_index, put a get_parent(), I can't see what I'm missing.... 5 0 Jan. 15, 2025
  • bullet rotationimmense-swallowIs it possible to shoot bullets based on the sprite's facing direction, instead of using the weapon pivot? How should I calculate the aim direction and rotation in this way? Thanks 1 0 Jan. 12, 2025
  • How to flip the gun object correctlyprivate-lemurHi, I am trying to make my own top down shooter with my custom character ( a floating robot ) with a gun. I am making the gun point in the direction of the mouse cursor and I am able to shoot bullets. The character is facing to the right direction and If I move my cursor to the left, I am able to flip the character sprite by utilizing the Sprite.flip H property. However, when I am doing the same with my gun, the position of the gun firepoint is staying the same and not getting aligned with Gun as I am only flipping the sprite texture and not the other nodes in the gun scene. I am thinking if I maybe rotate the entire gun node, it might solve it? According to you what's the best solution approach to fix this? 4 0 Jan. 09, 2025
  • Use the gamepad to playhumble-pelicanI found it a little inconvenient to use a gamepad for this game.For example, if my left hand is responsible for the direction of movement and my right thumb is responsible for the direction of shooting, then it is very inconvenient for me to press the X button to shoot. I think such a game experience is not very good in game design. If it is this type of game, how should we optimize it? Thank you ~ 2 0 Jan. 08, 2025
  • Hands behind the characterpetty-hareWhen i stop using the mouse or the gamepad, the hand are redrawn in front of the character, why ? 11 0 Jan. 05, 2025
  • My Solution for the aiming challenge.Abdul Hannan AhmedHi everyone. Since I don't have a game-pad, controller or joystick of any sort, I mapped the 4 arrow keys to `aim_left`, `aim_right`, `aim_up` and `aim_down` Input Actions. Here's my `weapon_pivot.gd` script: ```gdscript extends Node2D var _is_using_mouse: bool = false func _unhandled_input(event: InputEvent) -> void: if event is InputEventMouseMotion: _is_using_mouse = true elif event is InputEventKey: var is_gamepad: bool = ( event.is_action_pressed("aim_left") or event.is_action_pressed("aim_right") or event.is_action_pressed("aim_up") or event.is_action_pressed("aim_down") ) if is_gamepad: _is_using_mouse = false func _process(delta: float) -> void: if _is_using_mouse: var direction := global_position.direction_to(get_global_mouse_position()) rotation = direction.angle() else: var vector := Input.get_vector("aim_left", "aim_right", "aim_up", "aim_down") if vector.length() > 0.0: rotation = vector.angle() ``` 1 0 Jan. 05, 2025
  • I wanted to make some changes, is this fine?Lucas PscheidtSo... I wanted to make the player weapon unique, by having 2 spawner points, one at each hand, but I dindn't want to implement this logic in the Weapon script because other weapons could not be using that, so I tried to extend my Weapon logic into a new script, which I attached to a 2dNode called PlayerWeapon, with 2 markers 2d, cooldown timer and shoot sound node. I wanted to tweek the shoot() function to implement an additional logic that would see if there was any spaw_points available, and if there was, it would spawn the bullets there, alternating between spawn points. I don't know if this code is ideal but it is working fine so far, any suggestions or feedback? 8 0 Jan. 03, 2025
  • Aim challengesram876Hello! I got this code (I looked at the solution a bit). ```gdscript extends Node2D var is_using_gamepad := false: set = _set_cursor func _process(delta: float) -> void: var aim_dir := Vector2.ZERO if is_using_gamepad: aim_dir = Input.get_vector("aim_left", "aim_right", "aim_up", "aim_down") else: aim_dir = global_position.direction_to(get_global_mouse_position()) if aim_dir.length() > 0.0: rotation = rotate_toward(rotation, aim_dir.angle(), 8 * delta) func _unhandled_input(event: InputEvent) -> void: is_using_gamepad = ( event is InputEventJoypadMotion or event is InputEventJoypadButton ) func _set_cursor(check_input :bool) -> void: is_using_gamepad = check_input if is_using_gamepad: Input.set_mouse_mode(Input.MOUSE_MODE_HIDDEN) else: Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE) ``` I have a question. When you entered the direction vector, you assigned the initial value to vector. What is it made for? When I changed the z-index, as in your example, the order of rendering did not change, but if I changed the unit to minus one, the order changed if I either pointed the cursor up or moved the stick up. I did the hand rotation differently, and it's noticeable when I abruptly change the position of my hands from right to left or vice versa, the hands turn smoothly and they were still above the player's head. 6 0 Dec. 28, 2024
  • Why doesn't it work with local mouse position?jongtFor the first Challenge, I thought it would be easier to use get_local_mouse_position() in the Pivot Point script instead of calculating the distance to the global mouse position. I assumed this would just get the coordinates of the mouse relative to the Pivot Point (which I thought would be the same as the distance vector that you used). I found the function in the documentation and thought it would make more sense than using global mouse position. But the behaviour got really wonky when I tried it this way. Am I getting something wrong about the global vs local logic or there a different problem? 2 0 Dec. 27, 2024
  • Solution QuestionMoKTHey Nathan & Jad, so for my mouse rotation I used the look_at() I found in the Godot Docs which was a quick solution, any issues in using it as opposed to the direction_to()? ```gdscript func _process(delta: float) -> void: var look_direction := Vector2.ZERO if is_using_gamepad: look_direction = Input.get_vector("look_left", "look_right", "look_up", "look_down") if look_direction.length() > 0.1: rotation = look_direction.angle() else: look_at(get_global_mouse_position()) ``` 1 0 Dec. 26, 2024
  • The bullets only shoots to the rightJelielTesting in the weapon scene it shots to direction of the node, but in the player scene not. 4 0 Dec. 20, 2024
  • Does moving the arms with the joypad feel jittery to anyone else?icky-ibisWhen moving using a joypad, it seems the motion is jittery compared to the mouse? The input mapping as it is setup in my project settings are as below: ```gdscript "aim_left" : Joypad Axis 2 - (Right Stick Left, Joystick 1 Left) - All Devices "aim_right" : Joypad Axis 2 + (Right Stick Right, Joystick 1 Right) - All Devices "aim_up" : Joypad Axis 3 - (Right Stick Up, Joystick 1 Up) - All Devices "aim_down" : Joypad Axis 3 + (Right Stick Down, Joystick 1 Down) - All Devices ``` 1 0 Dec. 20, 2024
  • I don't see any difference in logic between your suggestion and my code...CptnNuggetsand yes, mine doesn't work ! The input with the gamepad works fine, not with the mouse. BUT... before implementing the gamepad, the mouse detection worked just fine (it's only the rotation that is the issue, not shooting). I know I am not using the direction_to() method, but it still works... outside the if statement... I tried with your code (no changes were needed considering my conventions)... and yours works ! I am baffled as I really can't see what I did wrong -_-' ```gdscript var input_is_joypad := false func _unhandled_input(event: InputEvent) -> void: if event is InputEventJoypadMotion or InputEventJoypadButton: input_is_joypad = true else: input_is_joypad = false func _process(delta: float) -> void: var aim_direction := Vector2.ZERO if input_is_joypad: aim_direction = Input.get_vector("aim_left", "aim_right", "aim_up", "aim_down") else: aim_direction = get_global_mouse_position() - global_position rotation = aim_direction.angle() ``` 4 0 Dec. 17, 2024
Site is in BETA!found a bug?