How to Transform.LookAt target the player?

I’ve got Transform.LookAt working fine in unity when I target the Main Camera object and run the scene but when I run in VRchat the object doesn’t track the player.

How do I target the player with my script?

You have a couple options to get a player’s position. First, VRCPlayerApi.position will give you the position of the playercontroller, which is actually down between the player’s feet. You can add some distance on the Y axis to get something more like where the middle of their body might be.

You can also use VRCPlayerApi.GetTrackingData() to get the tracking position of a player’s head. This one’s slightly tricky. For the local player in VR, this will give you the tracking data of that player’s HMD. For the local player in Screen mode, I think it gives you the camera location. For a remote player, it tries to give you the head bone of the armature. If the head bone doesn’t exist, though, you’ll get the position (0, 0, 0).

Both these options give you a Vector3, which is a valid parameter for Transform.LookAt. If you really want to have a transform to work with and not just a vector3, then you’ll need to have an empty gameobject that you update to move to the position and rotation of the player every frame.

Hope that helps!

Here’s another attempt without success.
It builds and runs but the object is still static.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VRC.SDKBase;

public class TransformLookAt : MonoBehaviour
{
    private VRCPlayerApi local_player;

    private void Start()
    {
        local_player = Networking.LocalPlayer;
    }

    void Update()
    {
        var target = local_player.GetPosition();
        transform.LookAt(target);
    }
}