Godot Version
Godot v4.4.1.stable.mono - macOS Sequoia (15.4.0) - Multi-window, 1 monitor - Metal (Forward+) - integrated Apple M3 Pro (Apple9) - Apple M3 Pro (11 threads)
Disclamer
Hello, I’m very new to Godot and gamedev - basically my second day playing around. But I’ve been programming on c# for few years so I do have some understanding of programming.
Question
I’ve been playing with “Your First 2D Game” from godot documentation.
After walking through this “Getting Started” I tried to make a special mob, which would do some sort of player tracking and chasing him. The tracking part works fine, but I struggle to understand why my mob doesn’t rotate towards the player but stays locked with his initial rotation. By the flickering I see on playthrough I consider that rotation is being set for one frame or so, but than returns back to initial. Any help will be appreciated. I tried to attach a short explanation video, but I can’t load mp4 files.
Lower I provide related code code:
// I do have a timer in main scene which triggers every 0.5 seconds
private void OnMobTimerTimeout()
{
var mobBoss = TryGetMobBoss();
if (mobBoss is not null)
{
var player = GetNode<Player>("Player");
mobBoss.Recalibrate(player.Position);
}
else if (_score % 15 == 0 && _score > 0)
{
SpawnBossMob();
}
}
// Retruns mob boss if he exists in the scene
private BossMob? TryGetMobBoss()
{
var bossMob = GetTree().GetFirstNodeInGroup("BossMob");
return bossMob as BossMob;
}
private void SpawnBossMob()
{
var bossMob = BossMobScene.Instantiate<BossMob>();
var mobSpawnLocation = GetSpawnLocation();
bossMob.Position = mobSpawnLocation.Position;
var player = GetNode<Player>("Player");
bossMob.Recalibrate(player.Position);
AddChild(bossMob);
}
private PathFollow2D GetSpawnLocation()
{
var mobSpawnLocation = GetNode<PathFollow2D>("MobPath/MobSpawnLocation");
mobSpawnLocation.ProgressRatio = GD.Randf();
return mobSpawnLocation;
}
// Recalibrate method in BossMob class
public void Recalibrate(Vector2 playerPosition)
{
var velocity = new Vector2(Speed, 0);
var newAngle = (playerPosition - Position).Angle();
// This does not rotate the mob for some reason
Rotation = newAngle;
LinearVelocity = velocity.Rotated(newAngle);
_lifeCycles--;
if (_lifeCycles == 0)
{
// TODO: puff animation
QueueFree();
}
}
Here is my MobBoss scene -
[gd_scene load_steps=6 format=3 uid="uid://c5r5rggmcpawg"]
[ext_resource type="Script" uid="uid://cms818jjbxb2y" path="res://BossMob.cs" id="1_6lbbi"]
[ext_resource type="Texture2D" uid="uid://se46a8wlr5g5" path="res://art/enemyWalking_1.png" id="1_wixse"]
[ext_resource type="Texture2D" uid="uid://co34uw0k2lfjp" path="res://art/enemyWalking_2.png" id="2_6lbbi"]
[sub_resource type="SpriteFrames" id="SpriteFrames_blbt7"]
animations = [{
"frames": [{
"duration": 1.0,
"texture": ExtResource("1_wixse")
}, {
"duration": 1.0,
"texture": ExtResource("2_6lbbi")
}],
"loop": true,
"name": &"walk",
"speed": 3.0
}]
[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_yjb3h"]
radius = 64.0
height = 136.0
[node name="BossMob" type="RigidBody2D" groups=["BossMob", "Mobs"]]
collision_mask = 0
gravity_scale = 0.0
script = ExtResource("1_6lbbi")
[node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="."]
scale = Vector2(0.75, 1)
sprite_frames = SubResource("SpriteFrames_blbt7")
animation = &"walk"
frame = 1
frame_progress = 0.606371
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
position = Vector2(-3, 0)
rotation = 1.5708
scale = Vector2(0.75, 0.75)
shape = SubResource("CapsuleShape2D_yjb3h")
[node name="VisibleOnScreenNotifier2D" type="VisibleOnScreenNotifier2D" parent="."]