3
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: 1. GameObject player = GameObject.Find("player"); 2. Transform playerTransform = player.transform; 3. Vector3 playerpos = playerTransform.position; 4. 5. playerpos = player.position 6. 7. Debug.Log("Player position is: " + playerObject.position ); 8. 9. I just want help finding player position, I want to do the rest on my own Add comment · Hide 3 robertbu · Oct 30, 2014 at 07:19 PM 0 Assuming 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. Little_Dizzle · Oct 30, 2014 at 09:04 PM 0 When I have using UnityEngine; using System.Collections;

How to Get Specific Position Player

Embed Size (px)

DESCRIPTION

unity tutorial

Citation preview

Page 1: How to Get Specific Position Player

How to get Specific x, y, z of PlayerI'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:

1. GameObject player = GameObject.Find("player");

2. Transform playerTransform = player.transform;

3. Vector3 playerpos = playerTransform.position;

4.

5. playerpos = player.position

6.

7. Debug.Log("Player position is: " + playerObject.position );

8.

9.

I just want help finding player position, I want to do the rest on my own

Add comment ·  Hide 3

robertbu · Oct 30, 2014 at 07:19 PM 0Assuming 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.

Little_Dizzle · Oct 30, 2014 at 09:04 PM 0When I have

using UnityEngine;

using System.Collections;

public class rotationalGravity : MonoBehaviour {

void Start () {

Page 2: How to Get Specific Position Player

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

robertbu · Oct 30, 2014 at 09:11 PM 0   ShareThat'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.