Asked by Coder

Make me a script for a monster slowly following the player, it will follow the player in straight paths always lookin at the player.

All Answers 1

Answered by GPT-5 mini AI
Which engine/language do you want this for? (Unity C#, Godot GDScript, Unreal, plain pseudocode, etc.)

Below are two ready-to-use examples — one for Unity (C#) and one for Godot (GDScript). Both make the monster:
- move in straight segments (it chooses a direction and travels straight for a short segment),
- always look at the player while moving,
- move slowly (adjustable speed).

Unity (C#)
- Attach this script to the monster GameObject. Assign the player Transform in the inspector.

```csharp
using UnityEngine;

public class SegmentFollower : MonoBehaviour
{
public Transform player; // assign in inspector
public float speed = 2f; // movement speed
public float rotationSpeed = 10f; // how fast the monster turns to look at player
public float segmentDuration = 0.8f; // seconds per straight segment
public float stopDistance = 1f; // stop when this close to player (optional)

private Vector3 moveDir;
private float segmentTimer;

void Start()
{
if (player == null) Debug.LogError("Player not assigned.");
RecomputeDirection();
}

void Update()
{
if (player == null) return;

// If close enough, stop
float dist = Vector3.Distance(transform.position, player.position);
if (dist <= stopDistance) return;

// Recompute straight movement direction at the start of each segment
segmentTimer -= Time.deltaTime;
if (segmentTimer <= 0f)
{
RecomputeDirection();
}

// Move in the stored straight direction (so path is straight during the segment)
transform.position += moveDir * speed * Time.deltaTime;

// Always look at the player (smoothly)
Vector3 toPlayer = player.position - transform.position;
if (toPlayer.sqrMagnitude > 0.001f)
{
Quaternion targetRot = Quaternion.LookRotation(toPlayer.normalized);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRot, rotationSpeed * Time.deltaTime);
}
}

void RecomputeDirection()
{
moveDir = (player.position - transform.position).normalized;
moveDir.y = 0f; // keep movement on horizontal plane; remove if 3D movement desired
if (moveDir.sqrMagnitude < 0.0001f) moveDir = transform.forward;
segmentTimer = segmentDuration;
}
}
```

Godot (GDScript)
- Attach to the monster Node (e.g., KinematicBody or CharacterBody). Set the player node path or drag in the inspector.

```gdscript
extends CharacterBody3D # or KinematicBody if Godot 3.x

@export_node_path("Node3D")
var player_path
var player: Node3D = null

@export var speed: float = 2.0
@export var rotation_speed: float = 6.0
@export var segment_duration: float = 0.8
@export var stop_distance: float = 1.0

var move_dir := Vector3.ZERO
var segment_timer := 0.0

func _ready():
if player_path:
player = get_node(player_path) as Node3D
if player == null:
push_error("Player not assigned.")
_recompute_direction()

func _physics_process(delta):
if player == null:
return

var dist = global_transform.origin.distance_to(player.global_transform.origin)
if dist <= stop_distance:
return

segment_timer -= delta
if segment_timer <= 0.0:
_recompute_direction()

# Move in the fixed straight direction for this segment
var velocity = move_dir * speed
move_and_slide(velocity) # or use move_and_collide depending on setup

# Always look at player (smooth)
var to_player = (player.global_transform.origin - global_transform.origin)
to_player.y = 0
if to_player.length_squared() > 0.0001:
var target = Transform3D().looking_at(global_transform.origin + to_player, Vector3.UP)
var current_basis = global_transform.basis
global_transform.basis = current_basis.slerp(target.basis, clamp(rotation_speed * delta, 0, 1))
```

Notes and suggestions
- Tweak speed, segmentDuration, and rotationSpeed to get the "slow" and "straight path" feel you want.
- Keeping movement independent from rotation (as shown) gives straight segments while the monster still visually looks at the player.
- If you want the monster to always update direction instantly and thus follow the player along a curved path, replace the segment logic with computing direction every frame and move along that (moveDir = (player.position - pos).normalized; pos += moveDir * speed * dt).
- If you want the monster to obey physics collisions, use Rigidbody/Character controllers accordingly (the example uses direct position updates or move_and_slide).

Tell me which engine you use (or if you want a different variant: grid-based straight moves, pathfinding-aware segments, instant look, or smooth look) and I can adapt the script.