I am making a tank shooting game that controls movement by left clicking the tank will move to the mouse position. But I want when the mouse is clicked, the tank body will slowly turn towards the mouse direction, after returning to the exactly mouse direction, the tank will move towards the mouse direction, also known as the target's direction. The same goes for the turret. So, how should I code, guys? Attached image is the code that I am using, so how do I add or correct it? Thank you for supporting!
Rotation and Movement
I wish people would stop posting images of their code and instead post their code so people can try it.
- Edited
Here you are! This is Tank Body's code. Now how do I edit or add to get what I want?
extends KinematicBody2D
var speed = 3000
var dichuyen = Vector2.ZERO
var target_tank = null
var selected = false
var chuot_vao = false
var moving = false
func _input(event):
if event.is_action_pressed("mouse_right"):
if selected:
target_tank = get_global_mouse_position()
moving = true
func _physics_process(delta):
# Nhap chuot vao chon tank
if Input.is_action_pressed("mouse_left") and chuot_vao:
selected = true
elif Input.is_action_pressed("mouse_left") and !chuot_vao:
selected = false
if target_tank:
if selected:
look_at(target_tank)
dichuyen = global_position.direction_to(target_tank) * speed * delta
$body_tank.play("run")
if global_position.distance_to(target_tank) < 5:
$body_tank.play("stand")
dichuyen = Vector2.ZERO
dichuyen = move_and_slide(dichuyen)
func _on_Control_tank_mouse_entered():
chuot_vao = true
func _on_Control_tank_mouse_exited():
chuot_vao = false
anybody helps?
- Edited
When you get your point input, a Vector2, set a flag or state on your tank to show that it needs to rotate. Also save the point input as that tank's rotation target. While it's in that state, have it rotate slowly in its _physics_process() toward the target. You can use angle_to_point to determine the angle the tank needs to rotate to.
Here's an example of slow rotation: https://godotengine.org/qa/61228/how-to-delay-my-rotation
When the tank's rotation is close to correct, change the state of the tank to movement, and add a vector to its position in _physics_process(). The vector can be based on "(target_point - tank_position).normalized()". As long as the rotation worked, it will look fine.
You're basically doing the click and move from the 2d movement tutorial, but in two, distinct stages.
Here's another way to do it with tweens. Since the tweens are executed in series, you don't have to do much state tracking.
# game.gd
extends Node2D
var player
func _input(event: InputEvent) -> void:
if event is InputEventMouseButton:
if event.pressed:
var pos = get_global_mouse_position()
player.new_target(pos)
func _ready() -> void:
player = Tank.new()
player.position = Vector2(200, 600)
player.rotation = PI
add_child(player)
# tank.gd
extends Sprite
class_name Tank
var target
var twn:SceneTreeTween
func _ready() -> void:
texture = preload('res://art/tank_blue.png')
rotation = PI
centered = true
func limit_ang(ang):
if ang > 2 * PI:
ang -= 2 * PI
elif ang < 0:
ang += 2 * PI
return ang
func new_target(tar):
target = tar
rotation = limit_ang(rotation)
var ang = target.angle_to_point(position)
ang -= PI / 2.0
ang = limit_ang(ang)
if abs(ang + 2 * PI - rotation) < abs(ang - rotation):
ang += 2 * PI
if abs(ang - (rotation + 2 * PI)) < abs(ang - rotation):
ang -= 2 * PI
if twn:
twn.stop()
twn = get_tree().create_tween()
twn.tween_property(self, 'rotation', ang, abs(ang - rotation) / 2.0)
twn.tween_property(self, 'position', target, position.distance_to(target) / 150.0)