Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | DesertOps | |
Old Version | Published before Godot 3 was released. |
So, as the title says I want to create shooting mechanics for the game I am making. I am having trouble in particular with the script for both the bullet and the player (I don’t know how to get the bullet to come out of the barrel of the gun). I drew inspiration for this game from Hotline Miami and I would like to make the shooting in my game resemble the shooting from Hotline Miami. I am a beginner and most of the script I have written until now was with the help of tutorials so bare with me. I will include screenshots and the script for both the bullet and the player.( I don’t know which parts to show you so I will paste the whole thing and hope nobody gets mad at me)
Player Script:
extends KinematicBody2D
export var MOTION_SPEED = 1000
const IDLE_SPEED = 10
var RayNode
var PlayerAnimNode
var anim = ""
var animNew = ""
onready var aspect_ratio = get_viewport_rect().size.x / get_viewport_rect().size.y
func _ready():
set_fixed_process(true)
RayNode = get_node("RayCast2D")
PlayerAnimNode = get_node("AnimatedSprite")
PlayerAnimNode = get_node("feet")
func _fixed_process(delta):
var motion = Vector2()
var mpos = get_global_mouse_pos()
look_at(mpos)
#Motion
if (Input.is_action_pressed("ui_up")):
motion += Vector2(0, -1)
RayNode.set_rotd(180)
if (Input.is_action_pressed("ui_down")):
motion += Vector2(0, 1)
RayNode.set_rotd(0)
if (Input.is_action_pressed("ui_right")):
motion += Vector2(1, 0)
RayNode.set_rotd(90)
if (Input.is_action_pressed("ui_left")):
motion += Vector2(-1, 0)
RayNode.set_rotd(-90)
motion = motion.normalized()*MOTION_SPEED*delta
move(motion)
#Animation
if (motion.length() > IDLE_SPEED*0.09):
if (Input.is_action_pressed("ui_right")):
anim = "Move"
anim = "move feet"
if (Input.is_action_pressed("ui_left")):
anim = "Move"
anim = "move feet"
if (Input.is_action_pressed("ui_up")):
anim = "move feet"
if (Input.is_action_pressed("ui_down")):
anim = "move feet"
else:
if (RayNode.get_rotd() == 180):
anim = "idle feet"
if (RayNode.get_rotd() == 0):
anim = "idel feet"
if (RayNode.get_rotd() == 90):
anim = "Idle"
anim = "idle feet"
if (RayNode.get_rotd() == -90):
anim = "Idle"
anim = "idle feet"
if anim != animNew:
animNew = anim
PlayerAnimNode.play(anim)
Bullet Script
extends RigidBody2D
var force = Vector2(0,0)
var source
var die_on_timeout = true
export var take_player_speed = 0.5
export var speed = 60.0
export var time_left = 10.0
export var damage = 1.0
var animationPlayer
export var dangerous = true
func _ready():
set_fixed_process(true)
animationPlayer = get_node("AnimationPlayer")
animationPlayer.connect("finished", self, "anim_player_finished")
func _process(delta):
time_left -= delta
if((time_left <= 0.0) and (die_on_timeout)):
dangerous = false
animationPlayer.play("die")
die_on_timeout = false
if (dangerous):
set_linear_velocity(get_linear_velocity() + force)
force = force.linear_interpolate(Vector2(0,0), delta*4)
for body in get_colliding_bodies():
if(body.has_method("damage")):
body.damage(source,damage)
dangerous = false
if (die_on_timeout):
animationPlayer.play("die")
func anim_player_finsihed():
if(!dangerous):
queue_free()
Sceenshots