- Home /
Duplicate Question
How to get Specific x, y, z of Player
I'm trying to find the x, y, and z of the player. I know this is probably easier than I think is but im a bit rusty on syntax. I'm writing in C#
I have my FirstPersonController called "player" and I need to draw a line from another game object called "defineUp", then I am going to apply force in that direction. I just need to know how to get the player position, specifically the z. I've tried:
GameObject player = GameObject.Find("player");
Transform playerTransform = player.transform;
Vector3 playerpos = playerTransform.position;
playerpos = player.position
Debug.Log("Player position is: " + playerObject.position );
I just want help finding player position, I want to do the rest on my own
Assu$$anonymous$$g your player is named 'player' with a lower case 'p', you can do:
Vector3 playerPos = GameObject.Find("player").transform.position;
Note that GameObject.Find() is not very efficient, and you should avoid executing it every frame. You would get the player transform in Start() and then do:
Vector3.playerPos = playerTransform.position;
...as you've done.
When I have
using UnityEngine;
using System.Collections;
public class rotationalGravity : $$anonymous$$onoBehaviour {
void Start () {
Vector3 playerPos = GameObject.Find("player").transform.position;
Vector3.playerPos = playerTransform.position; //does not contain definition for playerPos
}
void Update () {
Debug.Log("Player z is: " + playerPos);
}
}
it gives me that error for line 9
and how would I pull just the players z position
That's because you've not define 'playerTransform', and you are declaring playerPos.
At the top of the file:
private Transform playerTransform;
In Start():
playerTransform = GameObject.Find("player").transform;
Elsewhere in code when you need the position:
Vector3 playerPos = playerTransform.position;
Again, your player must be the only object with the exact anme 'player' for this to work.
Follow this Question
Related Questions
Camera rotation around player while following. 6 Answers
c# how to change player location on 0 hp 1 Answer
Instantiate bullet towards Player position 1 Answer
swap between 3 gameobjects mid game 0 Answers