Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Oct 30, 2014 at 07:19 PM by robertbu for the following reason:

Duplicate Question

avatar image
1
Question by Little_Dizzle · Oct 30, 2014 at 07:19 PM · positionplayerxyz

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

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Oct 30, 2014 at 07:19 PM 0
Share

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.

avatar image Little_Dizzle · Oct 30, 2014 at 09:04 PM 0
Share

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

avatar image robertbu · Oct 30, 2014 at 09:11 PM 1
Share

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.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

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

Stabilising users view in Unity VR (Fove HMD) 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges