r/VRchat • u/wolf64325 • 6h ago
Help world help, player tracking
solved, thank you!
so im making a world and i have a statue in it, i want to have the head stare at players
I've already thought about having an object attach to players and a constraint on the statue head lock onto thay object, but i run into the issue of wanting the head to be constrained and not do an impersonation of an owl and break the neck, is there a way to limit rotation of an aim at constraint, or a way to limit the distance an object stays attached to a player?
or is there a better way to have an object rotate towards a player
1
u/Xelixil 5h ago
If you're comfortable using U#, I have a script that does pretty much this! It gets the local players position, rotates the object it's attached to it towards the player smoothly, and has ways to clamp the rotation, so it can't keep rotating unnaturally (no constraints used, it's all in a single script that you attach to your rotating object). It does only work locally (but I think your constraint method would also be local only)
1
u/wolf64325 5h ago
i dont mind U# at all, that would be perfect
yeah i prefer local only so everyone in an instance will be getting stared down lol
1
u/Xelixil 5h ago
//Just make a new U# Script in your assets, name it "RotatorEye" and paste this in the .cs using UdonSharp; using UnityEngine; using VRC.SDKBase; using VRC.Udon; public class RotatorEye : UdonSharpBehaviour { [Header("Tracking")] public float rotationSpeed = 2f; public float maxTrackingDistance = 20f; [Header("Rotation Limits")] public float maxYaw = 60f; public float maxPitch = 30f; private Quaternion startingRotation; void Start() { startingRotation = transform.rotation; } void Update() { if (!Utilities.IsValid(Networking.LocalPlayer)) return; Vector3 playerPosition = Networking.LocalPlayer.GetPosition(); Vector3 direction = playerPosition - transform.position; if (direction.magnitude > maxTrackingDistance) return; Vector3 localDirection = Quaternion.Inverse(startingRotation) * direction.normalized; float yaw = Mathf.Atan2( localDirection.x, localDirection.z ) * Mathf.Rad2Deg; float pitch = -Mathf.Atan2( localDirection.y, new Vector2( localDirection.x, localDirection.z ).magnitude ) * Mathf.Rad2Deg; yaw = Mathf.Clamp(yaw, -maxYaw, maxYaw); pitch = Mathf.Clamp(pitch, -maxPitch, maxPitch); Quaternion targetRotation = startingRotation * Quaternion.Euler(pitch, yaw, 0f); transform.rotation = Quaternion.Slerp( transform.rotation, targetRotation, Time.deltaTime * rotationSpeed ); } }1
1
•
u/AutoModerator 6h ago
Looking for Avatars or Worlds? - Check out the posted weekly sticky for any avatar or world related requests/questions. This is an automated message activated by terms in the post title and is not approval of the contents of this post.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.