Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | tam0705 | |
Old Version | Published before Godot 3 was released. |
I have 2D topdown shooter game where there are zombies - and the player shoots them.
When I used “move_to” in the bullet script to check if it collided with a zombie when I shot them, it seemed that the bullet stopped in front of the zombie’s face and it meant the collision system worked.
Then I changed “move_to” to “set_pos” by adding bullet’s position little by little in an idle process to simulate real bullets’s movements, and added a code that would print something if the bullet was colliding with a zombie.
But it seemed that “set_pos” made the collision system fails. I didn’t get any printed text in the output.
Is there any ways to fix this? I also think to change the zombie and bullet to rigidbodies and use “set_linear_velocity” or others such as apply force, impulse, etc, instead of “set_pos”, but I really don’t understand how to use those codes to make the bullet shoots to the mouse pointer with static velocity.
Here’s my bullet’s code:
extends KinematicBody2D
var damage
var xmove
var ymove
onready var hand
=get_tree().get_root().get_child(0).get_node("player").get_node("hand")
const SCREEN_WIDTH = 960
const SCREEN_HEIGHT = 640
signal hit(damage)
func _ready():
set_pos(Vector2(hand.get_global_pos().x,hand.get_global_pos().y))
func shoot(attack,posx,posy):
damage = attack
look_at(Vector2(posx,posy))
xmove = (get_pos().x - posx) * -1 / 100
ymove = (get_pos().y - posy) * -1 / 100
if abs(xmove) && abs(ymove) < 1.5:
xmove *= 3
ymove *= 3
if abs(xmove) && abs(ymove) > 4:
xmove /= xmove / 4
ymove /= ymove / 4
print (xmove,ymove)
show()
set_process(true)
func _process(delta):
if is_colliding():
print("HI")
emit_signal("hit",damage)
queue_free()
set_pos(Vector2(get_pos().x+xmove,get_pos().y+ymove))
if get_pos().x < (0-SCREEN_WIDTH)/2:
queue_free()
elif get_pos().x > SCREEN_WIDTH/2:
queue_free()
if get_pos().y < (0-SCREEN_HEIGHT)/2:
queue_free()
elif get_pos().y > SCREEN_HEIGHT/2:
queue_free()
Thank you really much for helping, and sorry for my bad English.