Created
March 6, 2025 02:53
-
-
Save lamarmarshall/92f6952266067b88b6f502990620f674 to your computer and use it in GitHub Desktop.
godot, move charater, character controller, top down, shoot bullet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends CharacterBody2D | |
@onready var bullet_spawn_point: Marker2D = $BulletSpawnPoint | |
@onready var bullet = preload("res://bullet.tscn") | |
var speed: int = 75 | |
var direction: Vector2 = Vector2() | |
func _physics_process(delta: float) -> void: | |
var input_dir = Vector2(Input.get_axis("ui_left", "ui_right"), Input.get_axis("ui_up", "ui_down")).normalized() | |
if input_dir.x > 0: | |
get_node("Player").frame = 1 | |
get_node("Player").flip_h = false | |
direction = input_dir | |
elif input_dir.x < 0: | |
get_node("Player").frame = 1 | |
get_node("Player").flip_h = true | |
direction = input_dir | |
elif input_dir.y > 0: | |
#down | |
get_node("Player").frame = 0 | |
direction = input_dir | |
elif input_dir.y < 0: | |
#up | |
get_node("Player").frame = 2 | |
direction = input_dir | |
bullet_spawn_point.position = direction*5 | |
if Input.is_action_just_pressed("Shoot"): | |
var bullet_temp = bullet.instantiate() | |
if not direction: | |
direction.y = 1 | |
bullet_temp.velocity = direction * 100 | |
bullet_temp.global_position = bullet_spawn_point.global_position | |
get_node("Bullets").add_child(bullet_temp) | |
velocity = input_dir * speed | |
move_and_slide() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment