Asked by Coder

Make a MonsterBehavior script in Unity that makes a monster slowly follow and look at you over time.

All Answers 1

Answered by GPT-5 mini AI
Here's a simple, easy-to-use Unity C# script that makes a monster slowly rotate to look at the player and move toward them over time. It smooths the rotation with Slerp and moves forward only while outside a stop distance.

Create a new C# script named MonsterBehavior.cs and paste this code:

```csharp
using UnityEngine;

public class MonsterBehavior : MonoBehaviour
{
[Header("Target")]
public Transform target; // Assign player transform in inspector (or use tag auto-find below)

[Header("Movement")]
public float moveSpeed = 2f; // Movement speed (units/second)
public float stopDistance = 1.5f; // Distance at which the monster stops moving toward the target

[Header("Rotation")]
public float rotationSpeed = 5f; // Higher = faster turning

[Header("Options")]
public bool findPlayerByTag = true; // If true and target is null, tries to find GameObject tagged "Player"
public string playerTag = "Player";

// Optional: reference to animator to control walking animation
public Animator animator;
private readonly int animSpeedHash = Animator.StringToHash("Speed");

void Start()
{
if (target == null && findPlayerByTag)
{
GameObject p = GameObject.FindGameObjectWithTag(playerTag);
if (p != null) target = p.transform;
}
}

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

// Compute horizontal direction to player (ignore vertical difference for rotation)
Vector3 toTarget = target.position - transform.position;
Vector3 toTargetFlat = new Vector3(toTarget.x, 0f, toTarget.z);
float distance = toTargetFlat.magnitude;

// Smoothly rotate to face the player (only yaw / y-axis)
if (toTargetFlat.sqrMagnitude > 0.0001f)
{
Quaternion targetRotation = Quaternion.LookRotation(toTargetFlat.normalized);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
}

// Move forward while farther than stopDistance
if (distance > stopDistance)
{
Vector3 forward = transform.forward;
Vector3 newPosition = transform.position + forward * moveSpeed * Time.deltaTime;
transform.position = newPosition;

if (animator) animator.SetFloat(animSpeedHash, 1f);
}
else
{
if (animator) animator.SetFloat(animSpeedHash, 0f);
}
}

// Optional: draw stopDistance in editor for convenience
void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, stopDistance);
}
}
```

Usage notes:
- Attach this script to your monster GameObject.
- Set the target in the inspector to your player Transform, or set the player's Tag to "Player" (or change playerTag) and leave target null so it auto-finds.
- Tweak moveSpeed, rotationSpeed, and stopDistance to get the desired slow-following behavior.
- If you use an Animator, create a float parameter named "Speed" (or change the string in the script) to control walk/idle animations.
- If your monster uses Rigidbody physics, consider using Rigidbody.MovePosition and MoveRotation instead of directly setting transform.position/rotation (to avoid physics conflicts). If you want NavMesh obstacle-aware pathfinding, use Unity's NavMeshAgent instead — I can provide a NavMeshAgent version if you'd like.