Search Unity

  1. We would like to hear your feedback about Unity and our products. Click here for more information.
    Dismiss Notice

Getting an object's position/Transform without manually putting in X, Y and Z

Discussion in 'Scripting' started by DustyShinigami, May 24, 2019.

  1. DustyShinigami

    DustyShinigami

    Joined:
    Jan 5, 2018
    Posts:
    444
    I'm a bit confused with how to get an object's or a Transform's position without having to manually input the X, Y and Z coordinates. I have two public Transforms - startPoint and targetPoint - that I want an NPC to move to and from. So far I've had to manually input the coordinates for when the character goes to the target by putting target.transform.position = new Vector3(-12.45f, 12f, -0.6) and the same thing for the starting point. How would you get their positions in an easier way. It's supposed to be a pretty straightforward thing to do, but I can't figure it out still. :-\
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    4,547
    You keep saying "get", but then you describe setting the position.

    Anyways, that being said, you are setting the position correctly through code. So I don't understand what you are asking. For position you have to assign it a vector3 whether you declare a new one or you have an existing one. If you have two transform variables, then you can just set one objects position equal to the other objects position.

    Code (CSharp):
    1. target.transform.position = startTarget.position;
     
  3. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    1,236
    How do you want to set the position?
     
  4. DustyShinigami

    DustyShinigami

    Joined:
    Jan 5, 2018
    Posts:
    444
    I just figured there was a quicker way of setting (getting?) an object's position without having to manually get the coordinates, but if there isn't, that's fine. Was just wondering.
     
  5. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    1,236
    Well, what I do sometimes is create an empty game object and put it where I want in the scene. Then, for the script that needs to know the position, I create a reference to the empty object's transform.
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    6,181
    There are plenty of ways, but you will need to clearly define what you actually want to do for us to help you get to them. Do you want to set it based on some other object's position, for example?
     
  7. DustyShinigami

    DustyShinigami

    Joined:
    Jan 5, 2018
    Posts:
    444
    @kdgalla Yeah, that's what I've done in my scene for the startPoint and targetPoint the NPC moves to and from. So I'm not sure if those coordinates still need to be specified or if there's a simpler way.
     
  8. DustyShinigami

    DustyShinigami

    Joined:
    Jan 5, 2018
    Posts:
    444
    I'm just after my NPC to move to a specified location based on its Transform.
     
  9. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    1,661
    Define two points in code:
    Code (CSharp):
    1. [SerializeField]
    2. private Transform _pointA;
    3.  
    4. [SerializeField]
    5. private Transform _pointB;
    6.  
    7. // How to get position from any transform:
    8. Vector3 position = transform.position;
    9.  
    10. // How to set position to any transform:
    11. transform.position = position;
    If you drag & drop your points to the script you won't be needing to manually type in coordinates. Just retrieve position from _pointA or _pointB where needed.

    It seems like you're diving into a lot of code while being unprepared for it. Maybe its better to give a "Learn" section a try? Or at least some basic tutorials?
     
    DustyShinigami likes this.
  10. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    1,236
    You don't need to specify them explicitly. Create an object called StartPoint, for example, and put it anywhere in the scene using the editor. Then in your code add:
    Code (csharp):
    1. public transform StartPointTransform;
    or something like that. Now StartPointTransform.position is your position. You don't have to type the coordinates anywhere.

    This is useful because you may want to move the start position as you work on building your level. You won't have to change your code in that case.
     
    DustyShinigami likes this.
  11. DustyShinigami

    DustyShinigami

    Joined:
    Jan 5, 2018
    Posts:
    444
    Perfect. Thanks guys. At least this means I can remove a bit of code from my script. :)