• 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 /
avatar image
0
Question by lampshade · May 20, 2010 at 08:50 PM · gameobjectposition

Find A GameObject's Position

Hi,

I made a PreFab for one of my entities in Unity. I call the entity a type of "power source". This power source or entity, sits on ground and does not move, ever. Meaning that its position will never change. The player (a primitive cube game object) does move, and if the user chooses, will eventually become close to the power source.

I have declared a public GameObject within the scope of my Player.cs Script (the cube that the user gets to move around with). What I want to do is that if the player gets close to the power source, the script will know that.

Basically, what I am after are the coordinates, x, z, of the power source. Keep in mind that I have a y-up system and the power source will never be above the ground, so that axis is not a factor in (as you might have guessed) my distance formula.

So, essentially, I need to grab the coordinates of the power source. How would I do that?

using UnityEngine; using System.Collections;

public class Player : MonoBehaviour {

 // For When Player comes within Range of the powerSource.
 public GameObject PowerSourcePreFab;

 public float MoveSpeed = 10;
 public float RotateSpeed = 40;

 void Start()
 {
     // Intialize each power souce to 0% as this is the start of the match.
 }

 // Update is called once per frame
 void Update () 
 {
     // Amount to Move
     float MoveForward = Input.GetAxis("Vertical")  * MoveSpeed * Time.deltaTime;
     float MoveRotate = Input.GetAxis("Horizontal") * RotateSpeed * Time.deltaTime;

     // Move the player
     transform.Translate(Vector3.forward * MoveForward);
     transform.Rotate(Vector3.up * MoveRotate);

 }

}

To reiterate, I simply need to access the coordinates of the power source entity in my game.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by qJake · May 20, 2010 at 09:22 PM

Something's missing from your script, and you can't use your "public GameObject PowerSourcePrefab" like that. A prefab is just a template, it's not a reference to your "Power Source". It technically doesn't really have a "position".

You need to get references to all of the PowerSources in the scene, and iterate through those. Alternatively, you could (and this would be way easier), just place a script on each PowerSource to check if the player is in range, and then do something at that point in time.

Here's some pseudocode for a script you could place on your PowerSources:

// Pseudocode

float distance

// store the player object reference in here from the SCENE // using the editor inspector GameObject player

function Update if transform.position - player.position is less than distance then // do something here end if end function

Comment
Add comment · Show 3 · Share
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 lampshade · May 20, 2010 at 10:33 PM 0
Share

Thanks for the reply! I designed and coded this algorithm in C++ with Win32 API functions..Is their a tutorial you can provide that shows how to find a gameobject's type position?

avatar image qJake · May 21, 2010 at 12:11 AM 0
Share

Wait... you did what? This just needed to be coded inside of a Javascript or C# file inside Unity and attached to your game object? Noone said anything about C++? And it's not an "algorithm", either... And to find a game object's position, you just use gameObject.transform.position, why would you need a tutorial on that?

avatar image lampshade · May 21, 2010 at 04:47 AM 0
Share

All right. Its a little funky, but I managed to get it working and made a little print inside else print outside too. Also, incremented a int varaible when I was inside ;))

Thanks for your help.

avatar image
2

Answer by Kinifi · May 20, 2010 at 09:21 PM

I did something like this once and when my player walked into a trigger. The trigger would make an object play an animation. I believe you can use the same code I used and just alter it a little.

I used the following code:

function movemaze1()
{
maze1 = true
audio.PlayOneShot(Mazemovesound);
var maze1 : GameObject = GameObject.Find("maze");
maze1.animation.Play("mazeonemovetwo")
}

Comment
Add comment · Show 3 · Share
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 qJake · May 20, 2010 at 09:22 PM 0
Share

This has nothing to do with his question...

avatar image lampshade · May 20, 2010 at 11:03 PM 0
Share

if I could specify x, z coordinates in find, then I might be okay..

avatar image lampshade · May 21, 2010 at 04:48 AM 0
Share

$$anonymous$$inifi, Thanks for your help =)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Get position for each Gameobject (in array) ? 0 Answers

GameObject won't move to Transform Position 0 Answers

GameObject change Position after game started 1 Answer

Scrolling Sprite Object 0 Answers

How to make an object go to pre-seted position in world 1 Answer


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