How to find player Location?


I have a question about finding the position of a player with the name player. The player object is already declared in another script and I wish to know mainly how to find the object’s position of x y or z and what function to put it (update or start) and also how to output it to debug log as well. I want the position info to be updating in real time of the player

Hello there,

You say you already have a reference to the player, but since you don’t indicate the type I’ll write as if that reference didn’t exist.

• You first want to get a reference to the player on Start

• Then, if you constantly want to know where the player is, do this on Update (it will be called 60 times a second)

    private GameObject playerObj = null;

    private void Start()
    {
        //If you want to find it by NAME. For this you have to make sure you only have 1 object named "Player".
        if (playerObj == null)
            playerObj = GameObject.Find("Player");

        //If you want to find it by TAG. For this you have to make sure you give your player object the tag "Player".
        if (playerObj == null)
            playerObj = GameObject.FindGameObjectWithTag("Player");

        //If you want to find it by SCRIPT. For this you have to make sure your player has the proper script on it, and is the only one to have it.
        if (playerObj == null)
            playerObj = FindObjectOfType<PlayerScript>().gameObject;
    }

    private void Update()
    {
        Debug.Log("Player Position: X = " + playerObj.transform.position.x + " --- Y = " + playerObj.transform.position.y + " --- Z = " + 
        playerObj.transform.position.z);
    }

I hope that helps!

Cheers,

~LegendBacon

A quick way would be like this:

private GameObject player;

private void Awake(){
    player = GameObject.Find("PlayerNameHere");
}

Or you can just link the Player GameObject in the editor by making the variable “player” public.

Then just print its position like so:

private void Update()
{
    Debug.Log("Player position is: "+ player.transform.position);
}

i need help please my code is wrote like this but when i left click it at first takes me somewhere i dont want to go but then right click i can save any location in the game and left click it to go there all the time how do i change the first spawn location default location every time i click the button at first before saving a location please help my code is this

just do this @Anon127

 using System.Collections; using System.Collections.Generic; using UnityEngine;

public class MovePLayer : MonoBehaviour {

    public Rigidbody rb;

and add a RIgidbody to it and do rigidbidy.position