Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Tomi |
Hello!
In my program I create bullet instances at the player’s weapon, but these instances aren’t move.
Here the code where I create bullets:
extends Sprite
var bullet=preload(“res://robflovedeke.tscn”)
var dir=Vector2()
func _process(delta):
if get_global_mouse_position().x<self.position.x:
dir = (get_global_mouse_position()-self.position).angle()+180
self.rotation=dir
if (Input.is_mouse_button_pressed(BUTTON_LEFT)):
var l=bullet.instance()
get_parent().add_child(l)
l.position=position
l.rotation=dir
And the other code in the bullet node:
extends KinematicBody2D
var itsspeed = 4
func _physics_process(delta):
var collideobj=move_and_collide(Vector2(-itsspeed*delta,0))
if position.x<get_viewport_rect().size.x/2: #get_viewport().size/2
queue_free()
elif (collideobj):
if collideobj.get_collider()==“ellenseg”:
collideobj.get_collider().queue_free()
queue_free()
Bullets are created well, but not moving. What is wrong with my code?
4*delta is 4 pixels per second. Are you sure it’s not moving at all and not just moving very slowly? Increase itsspeed to 400 and see if that helps.
dir = (getglobalmouse_position()-self.position).angle()+180
angle()
returns radians and the rotation
propery expects radians, you need to deg2rad(180)
to make this work how you want it to
timothybrentwood | 2021-05-09 15:34
I increased “itsspeed” variable, but the situation is the same: the bullet is created on the weapon sprite and doesn’t move.
I thought it is because of collision between bullet and weapon, therefore I changed initial position X and Y values of bullet onto a collison free place, but the bullets are stand in one place. :-/
Tomi | 2021-05-10 12:24