using UnityEngine; namespace Nomadro.Minigame { /// /// Third-person XZ motor. Feed stick via SetMoveInput; also reads WASD in Editor. /// public class PlayerMotor : MonoBehaviour { public float speed = 5.8f; public float boundRadius = 22f; public float turnSmooth = 12f; public float sprintMul = 1.45f; Vector2 _stick; Vector3 _vel; public void SetMoveInput(Vector2 stick) { _stick = Vector2.ClampMagnitude(stick, 1f); } void Update() { var ix = _stick.x; var iz = _stick.y; #if UNITY_EDITOR || UNITY_STANDALONE if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)) iz += 1f; if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow)) iz -= 1f; if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) ix -= 1f; if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) ix += 1f; #endif var dir = new Vector3(ix, 0f, iz); if (dir.sqrMagnitude > 1f) dir.Normalize(); var mul = 1f; #if UNITY_EDITOR || UNITY_STANDALONE if (Input.GetKey(KeyCode.LeftShift)) mul = sprintMul; #endif _vel = Vector3.Lerp(_vel, dir * (speed * mul), 1f - Mathf.Exp(-turnSmooth * Time.deltaTime)); transform.position += _vel * Time.deltaTime; var flat = new Vector3(transform.position.x, 0f, transform.position.z); if (flat.magnitude > boundRadius) { flat = flat.normalized * boundRadius; transform.position = new Vector3(flat.x, transform.position.y, flat.z); } if (_vel.sqrMagnitude > 0.01f) { var look = Quaternion.LookRotation(_vel.normalized, Vector3.up); transform.rotation = Quaternion.Slerp(transform.rotation, look, 1f - Mathf.Exp(-10f * Time.deltaTime)); var bob = Mathf.Abs(Mathf.Sin(Time.time * 12f)) * 0.06f; var p = transform.position; p.y = bob; transform.position = p; } } } }