The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

0 votes

Hi all.

I have an issue I've been trying to solve, but haven't much success. Before I messed with my code a bit, I had a hostile ship that would turn towards the player, and fly towards them. However fidgety it was, it, for the most part, worked.

One attempt at refactoring later, and I've taken a minor step back, returning to the unstopping spinning-top once more. It only turns clockwise, which will be an issue when the player is only a short left turn away, as the ship appears to love right turns.

extends RigidBody2D

signal searching
signal turning
signal moving

enum states {

export var move_thrust = Vector2(175,0)
export var torque = 2500
var has_target = false
var target_list = ["none"]
var current_state = "SEARCH"
var player_pos = Vector2()
var in_pursuit_area = false
var thruster_active = false
var is_turning = false
var is_searching = true
var d
var player

func _ready():

func _integrate_forces(state):
    var ship_pos = self.global_position
    var ship_rot = self.global_rotation
    var speed = self.get_linear_velocity()
    var spin = self.get_angular_velocity()

    # SEARCH
    if is_searching == true:
        if speed < Vector2(0,0) or speed > Vector2(0,0):
        if spin < 0 or spin > 0:

    # ROTATE
    if is_turning == true:
        var rot_dir = 0
        var angle_rad = self.get_angle_to(player_pos)
        var angle_deg = rad2deg(angle_rad)
        var w = (d * 1000) / angle_deg
        var new_degrees = lerp(0, angle_deg, w)
        var new_rad = deg2rad(new_degrees)
        if new_rad > 0:
            rot_dir = 1
        if new_rad < 0:
            rot_dir = -1
        set_applied_torque(torque * rot_dir)

    # MOVE
    if thruster_active == true:
    if thruster_active == false:

func _on_PursuitRange_body_entered(body):
    if body.is_in_group("Player"):
        in_pursuit_area = true
        player = body

func _on_SearchRange_body_entered(body):
    if body.is_in_group("Player"):
        has_target = true
        if target_list.size() == 1:

func _on_PursuitRange_body_exited(body):
    if body.is_in_group("Player"):
        if target_list.size() > 1:
            has_target = false
            in_pursuit_area = false

func _process(delta):
    d = delta

    if has_target == true and in_pursuit_area == true:
        if player_pos != player.global_position:
            player_pos = player.get_global_position()


            if target_list.size() > 1:
                current_state = "ROTATE"

            if target_list.size() == 1:
                current_state = "SEARCH"

            if $MaxPursuitCaster.is_colliding() == true and has_target == true:
                var collider = $MaxPursuitCaster.get_collider()
                if collider.is_in_group("Player") and in_pursuit_area == true:
                    is_turning = false
                $Thrusters/Engine.emitting = false
                thruster_active = false
                current_state = "ROTATE"


func _on_EnemyTemplate_body_entered(body):
    if body.is_in_group("Singularity"):
        current_state = "DEAD"

func _on_EnemyTemplate_searching():
    is_searching = true

func _on_EnemyTemplate_turning():
    is_turning = true

func _on_EnemyTemplate_moving():
    thruster_active = true
    $Thrusters/Engine.emitting = true

Now I find myself at a minor impasse. Most of the game is finished, and the movement of the hostile ships is crucial for gameplay. Any suggestions on how to get the ROTATE and MOVE states working again?

in Engine by (199 points)

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.