nomadro/apps/minigame-unity/Assets/Scripts/Game/NpcWander.cs
Eric 58a845db32 Add WeChat minigame triple-engine scaffold with Godot Chiang Mai plaza SoT.
Unity and Cocos stay as parallel schemes; apps/minigame ships the Godot 4.7 WeChat pack for DevTools.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-06 20:17:14 +08:00

52 lines
1.5 KiB
C#

using UnityEngine;
namespace Nomadro.Minigame
{
/// <summary>Idle / light wander NPC that faces the player.</summary>
public class NpcWander : MonoBehaviour
{
public Transform lookAt;
public bool wander = false;
public float radius = 1.5f;
public float sway = 6f;
float _seed;
Vector3 _origin;
void Awake()
{
_seed = Random.value * 10f;
_origin = transform.position;
}
void Update()
{
if (wander)
{
var t = Time.time * (0.35f + _seed * 0.03f) + _seed;
var p = _origin;
p.x += Mathf.Cos(t) * radius;
p.z += Mathf.Sin(t * 0.85f) * radius;
transform.position = Vector3.Lerp(transform.position, p, Time.deltaTime * 1.2f);
}
if (lookAt != null)
{
var dir = lookAt.position - transform.position;
dir.y = 0f;
if (dir.sqrMagnitude > 0.01f)
{
var look = Quaternion.LookRotation(dir.normalized, Vector3.up);
transform.rotation = Quaternion.Slerp(transform.rotation, look, Time.deltaTime * 3f);
}
}
else
{
var e = transform.eulerAngles;
e.y += Mathf.Sin(Time.time + _seed) * sway * Time.deltaTime;
transform.eulerAngles = e;
}
}
}
}