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.
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
```60Jan. 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....
50Jan. 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? Thanks10Jan. 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? 40Jan. 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 ~20Jan. 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 ?110Jan. 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()
```10Jan. 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?80Jan. 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.60Dec. 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?20Dec. 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())
```10Dec. 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.
40Dec. 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
```10Dec. 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()
```40Dec. 17, 2024
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.