|
|
|
|
Reply From: |
Footurist |
Are you familiar with instancing? If not, it’s pretty easy.
I would recommend making your projectile a scene: (right click → save branch as scene), which you then can instance an infinite amount of times. That means, you could spawn your projectiles in some kind of container (simply Node2D
):
In the container node:
var projectile_scene = load("path_to_the_scene_in_resources")
var projectile = projectile_scene.instance()
add_child(projectile)
After you throw them and they get stuck, you would remove them from the container
projectile.queue_free()
and instance them as a child of the Area2D with accurate position in the same manner. This way, it would simply obey the Area2D’s rotation.
Ok I went with Footurist’s approach because it looked simpler. I also made some changes.
Target is now rotated as Area2d with CollisionShape2D. It has script:
extends Area2D
func _physics_process(delta):
rotate(2 * delta)
Next I have a bullet with code:
extends Node2D
var speed = 800
signal bullet_hit
func _physics_process(delta):
position.y -= delta * speed
if $RayCast.is_colliding():
emit_signal("bullet_hit", $RayCast.get_collision_normal(), $RayCast.get_collision_point())
hide()
queue_free()
As you can see I am using RayCast2D to check for collision.
Finally I have the world where everything is occurring.
extends Node
var bulletScene = preload('res://Scenes/Bullet.tscn')
var bulletStickScene = preload('res://Scenes/BulletStuck.tscn')
func _input(event):
if Input.is_action_pressed("mouse_press"):
var bullet = bulletScene.instance()
bullet.global_position = $Spawn.global_position
bullet.connect("bullet_hit", self, "bullet_hit_callback")
add_child(bullet)
func bullet_hit_callback(hit_pos_normal, hit_pos):
var bulletStuck = bulletStickScene.instance()
bulletStuck.global_position = hit_pos * hit_pos_normal - Vector2(0, 110)
$Target.add_child(bulletStuck)
The result is almost what I need. The Bullet gets stuck to the Target but every time on the same position as if Target didn’t move at all.
Again, thank you for help and suggestions
Cheers
ricma985 | 2018-02-09 22:23
Upload project file, I’ll have a look at it. WIll be easier.
Footurist | 2018-02-10 10:14
Here it is.
ricma985 | 2018-02-10 11:48
This is working as intended now. I’ve added some useful stuff and removed the unnecessary. Play around with it to figure out why it’s doing what it does. I’ve added an animation “fade_out” for the bullet, which triggers a function “_destroy_self” at its end for example.
Footurist | 2018-02-10 16:16
That’s it, thank you so much!
ricma985 | 2018-02-10 20:44
No problem. Remember to deduce the logic, so that you remember it later on.
Footurist | 2018-02-10 21:56