Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Gary |
Hi All,
I hope you’re all well. I’m new to Godot, well I am new to game dev too. I can’t code first up, I have followed a tutorial (UmaiPixel 2D platformer series) on line which for me was a perfect starting point. I did learn a lot, I understand why and what he is coding but going solo is a differnt story, I am watching many online tutorials and slowly, it’s sinking in. I have the player able to fire and kill enemies but because I’m not sure on the coding side I can’t reverse engineer what I have learned in order to have enemies detect and fire back at the player (My game is a platformer)
I understand that I need a enemy fire animation, an area 2D where the enemy fires from, a collision detector on the enemy for when the player enters that area, I only know this because that’s similar to what I did for the player, without the detector. I just can’t code though. I have made a backup folder so I can test my game incase I broke it, which I knew I would. Any help with the code side of things on this matter would be very very much appreciated. Below is the code for the player and the enemy, sorry for the mess.
**Player**
extends KinematicBody2D
const SPEED = 60
const GRAVITY = 5
const JUMP_POWER = -150
const FLOOR = Vector2(0, -1)
const FIREBALL = preload(“res://Fireball02.tscn”)
const LASERBLUE = preload(“res://LaserBlue.tscn”)
var velocity = Vector2()
var on_ground = false
var is_attacking = false
var is_dead = false
var fireball_power = 1
func _physics_process(delta):
if is_dead == false:
if Input.is_action_pressed("ui_right"):
if is_attacking == false or is_on_floor() == false:
velocity.x = SPEED
if is_attacking == false:
$AnimatedSprite.play("run")
$AnimatedSprite.flip_h = false
if sign($Position2D.position.x) == -1:
$Position2D.position.x *= -1
elif Input.is_action_pressed("ui_left"):
if is_attacking == false or is_on_floor() == false:
velocity.x = -SPEED
if is_attacking == false:
$AnimatedSprite.play("run")
$AnimatedSprite.flip_h = true
if sign($Position2D.position.x) == 1:
$Position2D.position.x *= -1
else:
velocity.x = 0
if on_ground == true && is_attacking == false:
$AnimatedSprite.play("idle")
if Input.is_action_pressed("ui_up"):
if is_attacking == false:
if on_ground == true:
velocity.y = JUMP_POWER
on_ground = false
if Input.is_action_just_pressed("ui_focus_next") && is_attacking == false:
$LaserSound.play()
if is_on_floor():
velocity.x = 0
is_attacking = true
$AnimatedSprite.play("attack")
var fireball = null
if fireball_power == 1:
fireball = FIREBALL.instance()
elif fireball_power == 2:
fireball = LASERBLUE.instance()
if sign($Position2D.position.x) == 1:
fireball.set_fireball_direction(1)
else:
fireball.set_fireball_direction(-1)
get_parent().add_child(fireball)
fireball.position = $Position2D.global_position
velocity.y += GRAVITY
if is_on_floor():
if on_ground == false:
is_attacking = false
on_ground = true
else:
if is_attacking == false:
on_ground = false
if velocity.y < 0:
$AnimatedSprite.play("jump")
else:
$AnimatedSprite.play("fall")
velocity = move_and_slide(velocity, FLOOR)
if get_slide_count() > 0:
for i in range(get_slide_count()):
if "Enemy" in get_slide_collision(i).collider.name:
dead()
func dead():
is_dead = true
velocity = Vector2(0, 0)
$AnimatedSprite.play(“dead”)
$CollisionShape2D.disabled = true
$Timer.start()
func _on_AnimatedSprite_animation_finished():
is_attacking = false
func _on_Timer_timeout():
get_tree().change_scene(“res://TitleScreen.tscn”)
func gun_power_up():
fireball_power = 2
Enemy
extends KinematicBody2D
const GRAVITY = 10
const FLOOR = Vector2(0, -1)
var velocity = Vector2()
var direction = 1
#var death = preload(“res://Explosion.tscn”)
#var collision = move_and_slide(velocity *delta)
var is_dead = false
export(int) var speed = 30
export(int) var hp = 1
export(Vector2) var size = Vector2(1, 1)
func _ready():
scale = size
pass
func dead(damage):
hp = hp - damage
if hp <= 0:
is_dead = true
velocity = Vector2(0, 0)
$AnimatedSprite.play(“dead”)
#$explosion.play()
# following line does not work in v3.1
# $CollisionShape2D.disabled = true
$CollisionShape2D.set_deferred(“disabled”, true)
$Timer.start()
if scale > Vector2(1, 1):
get_parent().get_node(“ScreenShake”).screen_shake(1, 10, 100)
func _physics_process(delta):
if is_dead == false:
velocity.x = speed * direction
if direction == 1:
$AnimatedSprite. flip_h = false
else:
$AnimatedSprite. flip_h = true
$AnimatedSprite.play("walk")
velocity.y += GRAVITY
velocity = move_and_slide(velocity, FLOOR)
if is_on_wall():
direction = direction * -1
$RayCast2D.position.x *= -1
if $RayCast2D.is_colliding() == false:
direction = direction * -1
$RayCast2D.position.x *= -1
if get_slide_count() > 0:
for i in range (get_slide_count()):
if "Player" in get_slide_collision(i).collider.name:
get_slide_collision(i).collider.dead()
func _on_Timer_timeout():
queue_free()
Have you tried using raycast?
Sinowa-Programming | 2020-06-18 03:11
I have followed a tutorial
Why is it that people always mention this but never mention which tutorial?
I understand that I need a enemy fire animation
If you want to animate the enemy character: yes. Otherwise: no. Given that you are just starting out and seem to have trouble to stay on top of things, I’d recommend you forego animating the enemy for now. Keep things simple!
an area 2D where the enemy fires from
If your enemy is an Area2D-node: yes. But in reality all you need is a position to fire from. If that’s the position of an Area2D or something else doesn’t matter!
a collision detector on the enemy for when the player enters that area
Unless you want your enemies to fire at your player regardless of how far away or close by your player is. Which, again, I would recommend as it’s simpler.
I just can’t code though.
If you really can’t code at all then I recommend you work on that first! There is no point in solving a problem for you when you don’t actually get the solution. All that would teach you is to come back and ask about your next problem as well.
If you have any concrete questions, I’ll be glad to help you out!
Any help with the code side of things on this matter would be very very much appreciated.
Helping you with code is a lot easier if you provide your attempts so far, the scripts you have, the nodes you’re using or even better the whole project.
njamster | 2020-06-18 13:24
Thanks njamster,
Thanks for taking the time to comment on individual sections of my issue, you're right I do need to get better at coding and this is what my main focus is. I'm watching tutorials from many different guys at the moment, including KidsCanCode, it's beginning to sink in little by little. I'm 47 years old in September, so it might take a little longer than, say for a much younger me.
I will keeep going though as I love game dev, I'm an Archectural Visualiser by trade, so working in 3D is second nature, but the coding side is my kryptonite. I have make and puplish a finished game at number 1 on my bucket list, I know I won't make a heap of money it's something I would like to be able to say, "I have made a game". and again you're right, If I haven't written the code or learned how then all I have done is created assets and used someone elses code to make my game.
Thanks again for you're comments and time, take care and stay safe.
Gary
Gary | 2020-06-22 15:57