How to get vector3 location of first person controller?

Unity nub here, this is my first question and I’m hoping since I was unable to locate the answer here or elsewhere that this quite obvious nub question isn’t going to be met with a slew of experts telling me to google it. It has to be simple, and I’m probably an idiot, but I can’t figure it the hell out and I just spent 30 minutes on Bing looking for this to no avail.

I’m scripting in C# btw (Can’t stand JS syntax).

Basically, I have a First Person Controller and a Cube object in my little scene I’ve got. I want to have the cube hover above the player (for research and learning purposes). So far I’ve made it rotate all sorts a funky ways with transform.rotate(Vector3), but when I tried to do transform.position(position of camera + .5y*) I realized I can’t figure out how to get the current Vector3 position of the Controller.

Since the first person controller is on the camera, assuming you are using the main camera and did not change the tag on the main camera, you can do this on the script on the cube:

transform.position = Camera.main.transform.position + Vector3.up * 0.5;

The typical ways to handle this situation (getting access to other game objects):

  • Create a public GameObject variable in the script and drag and drop the game object you want to get access to.
  • Create a private GameObject instance variable and find the game object by name using GameObject.Find() in Start().
  • Create a private GameObject instance variable and find the game object by tag using GameObject.FindWithTag() in Start().

Note since your cube code is following the first person controller, consider putting the code in LateUpdate() rather than Update().