Hello guys, can you help me.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Neloshok

Can you help me to make my tank to shoot at 4 directions, when i was
Looking for the answer there were nothing.

:bust_in_silhouette: Reply From: djmick

Okay, I can definitely help you as this shouldn’t be too hard, but you’re going to have to give me some more context on what you want to make. Do you want the tank to move around? How do you want it to shoot? With which button? How do you want it to decide which direction to shoot? If you just give me some more context I can help you make it. Just reply to this and I’ll help you as soon as I can.

No, i want tank just moving in 4 direction and shoot this 4 directions.
I want it to shoot at the direction my player watching at and when it’s collision with something it will disappear.
With spacebar.
I am already answered at this question:).

Neloshok | 2021-02-10 04:20

Okay so I would put all enemies into a group called “enemies”, and then do add a raycast2d to the player, and finally make an input called shoot, then use this code:

extends KinematicBody2D

onready var ray = $RayCast2D

var speed = 200

var velocity = Vector2()

func get_input():
    velocity = Vector2()
    if Input.is_action_pressed('right'):
        velocity.x += 1
        ray.cast_to = Vector2(200, 0)
    elif Input.is_action_pressed('left'):
        velocity.x -= 1
        ray.cast_to = Vector2(-200, 0)
    elif Input.is_action_pressed('down'):
        velocity.y += 1
        ray.cast_to = Vector2(0, 200)
    elif Input.is_action_pressed('up'):
        velocity.y -= 1
        ray.cast_to = Vector2(0, -200)
    velocity = velocity.normalized() * speed

func _physics_process(delta):
    get_input()
    velocity = move_and_slide(velocity)
    if Input.is_action_just_pressed("shoot") && ray.is_colliding():
        var object = ray.get_collider()
        if object.is_in_group("enemies"):
            object.queue_free()

This will make it so the tank shoots in the direction it is facing and if there is an enemy in front of it it will kill it. You won’t be able to see a bullet yet, but hopefully this helps.

djmick | 2021-02-10 15:42

Sorry, but i am using position2d on my character and i have a bullet in new scene, but i don’t know how to make my position2d rotate to position my player facing at because i don’t rotate tanks sprite i rotate it by animation of idle, and i want you to help me.
This site i was watching at how to make bullet and shoot:
http://kidscancode.org/godot_recipes/2d/2d_shooting/

Neloshok | 2021-02-10 17:06

Oh, I’m sorry that I misunderstood your question! So do you need help just moving the position2d to a new part of the character, or do you need help making the bullets actually shoot in the direction the tank is moving? If you copy and paste your tank’s movement code in here, I could help you do it, but it is kind of hard without your code. Again, I’m sorry for the misunderstanding and that this is taking so long.

djmick | 2021-02-10 17:36

I think I that if i just move position2d in right direction and rotate it for 90 degrees on every direction, it will work, but I need to do this while my game is running and I don’t know how to do this?

This is code for my player

extends KinematicBody2D

var speed = 100 # speed in pixels/sec
var velocity = Vector2.ZERO
export (PackedScene) var Bullet

onready var animationPlayer = $AnimationPlayer
onready var animationTree = $AnimationTree

func get_input():
velocity = Vector2.ZERO
if Input.is_action_pressed(‘ui_right’):
velocity.x += 1
elif Input.is_action_pressed(‘ui_left’):
velocity.x -= 1
elif Input.is_action_pressed(‘ui_down’):
velocity.y += 1
elif Input.is_action_pressed(‘ui_up’):
velocity.y -= 1
if Input.is_action_just_pressed(‘shoot’):
shoot()

velocity = velocity.normalized() * speed

func shoot():
var b = Bullet.instance()
owner.add_child(b)
b.transform = $Muzzle.global_transform

func _physics_process(delta):
get_input()
velocity = move_and_slide(velocity)
if velocity != Vector2.ZERO:
animationTree.set(“parameters/Idle/blend_position”, velocity)

This is code for my bullet

extends Area2D

var speed = 150

func _physics_process(delta):
position += transform.x * speed * delta

func _on_Bullet_body_entered(body):
if body.is_in_group(“mobs”):
body.queue_free()
queue_free()

Neloshok | 2021-02-11 02:32

On your position2d, you should set its position to the center of the player, then set its offset so that it lines up with where you want it. Then, when you rotate it, it will rotate around the center of your player, and transform it so the bullet will work. Under your 5 inputs for directions, set the position 2d’s rotation to what it should be to point in the direction it should, you’ll have to test this to see. One last thing; I’m on my phone right now so I can’t check, but position2d’s transforms can be finicky and I can’t remember if you can rotate them, so you might have to substitute it for a node2d. Hopefully this works, if you have any questions just ask

djmick | 2021-02-11 04:06

Do you know how to rotate position 2d while game is running and how can i change lay of nodes, like now i have my tank below bullets
But i want my tank was above bullet?

Neloshok | 2021-02-11 06:16

Do you have discord to messeng, because this is very uncomfortable to messege like this.

Neloshok | 2021-02-11 06:19