Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

I need some help moving an object to X,Y,Z position

Discussion in 'Getting Started' started by sadsack, May 17, 2019.

  1. sadsack

    sadsack

    Joined:
    May 27, 2015
    Posts:
    124
    Ok, here we go, I have this space ship, (Well don't every body have one) I want to be able to input XYZ position and have the spaceship then and face that position and then move to it at a given speed. When it get to that position it stops.

    Like this
    input X 232.89
    input Y 112.9
    input Z 23
    input speed 2
    That is a position in my 3D space. I want my ship to got to that position, then stop when it get there.
    I know I am going to have to get into transform and translate, but I don't know how to do that.
    Then I will need an if statement to stop the ship.
    So maybe some one can point me in the right dri.
    Thank you
    Renny
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    7,893
    When faced with a big problem, you need to break it down into smaller problems, and keep doing that until they are so small you are able to solve one.

    In this case, the sub-problems are: (1) how to input some numbers? (2) How to point an object at a 3D position? (3) How to move forward (or towards a goal) at a constant speed until you get there?

    The answers are probably (1) using a series of UI.InputFields; (2) using Transform.LookAt; and (3) using Vector3.MoveTowards. (You don't actually need any 'if' statement, because MoveTowards does nothing once the thing you're moving is already at the target position.)

    If you're very new, these answers may not be detailed enough for you. In that case pick just one of them, research it, try some things out, and then post back with a description of what you're trying to do, what you tried, and how it failed.
     
    Homicide likes this.
  3. sadsack

    sadsack

    Joined:
    May 27, 2015
    Posts:
    124
    Thank you Joe Strout
     
  4. sadsack

    sadsack

    Joined:
    May 27, 2015
    Posts:
    124
    I am coming along so so. I have not got my nav. system working yet. I am close I think. But first I need tp display the x,y,z
    of a game object(my space ship) on the screen. I looked all over for a tut to do that. Did not find one. I want the xyz to be displayed ui on the screen in real time. Can that be done?
    thank you
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    7,893
    I'll assume you've already gone through the UI tutorials. If not, go do so now.

    So you know how to put a UI.Text on a Canvas. Then you just need to update that Text with the position of your object. To do that, you could attach a script to your object that looks something like this:

    Code (CSharp):
    1. using UnityEngine.UI;
    2.  
    3. public class PositionDisplay : MonoBehaviour {
    4.     public Text displayText;
    5.    
    6.     void Update() {
    7.         displayText.text = transform.position.ToString();
    8.     }
    9. }
     
  6. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    13,872
    When searching for tutorials you generally want to avoid searching for a complex subject (or multiple simple subjects) as most tutorials are made to cover only one simple subject. How to show a coordinate in a UI, for example, is best found by searching for tutorials on how to use Vector3 and on how to use Unity's Text UI component.

     
    JoeStrout likes this.
  7. sadsack

    sadsack

    Joined:
    May 27, 2015
    Posts:
    124
    Hi,
    Well i been working on this all day, it is 9:40pm and I stared at 8:00am.
    I have not try your code yet. Just log into the forum. this is what I been working with that I came up with.
    It does not work right.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;

    public class dis_x_y_z : MonoBehaviour
    {
    public Text text_X;
    public Text text_Y;
    public Text text_Z;
    // Use this for initialization
    void Start ()
    {

    }

    // Update is called once per frame
    void Update ()
    {
    text_X.text = "X Axis :" + transform.position.x.ToString();
    text_Y.text = "Y Axis :" + transform.position.y.ToString();
    text_Z.text = "Z Axis :" + transform.position.z.ToString();
    }


    }

    it give the position but does not up date.
    You can see on the photo it come up but does not update
     

    Attached Files:

    • xyz.png
      xyz.png
      File size:
      327.1 KB
      Views:
      36
  8. sadsack

    sadsack

    Joined:
    May 27, 2015
    Posts:
    124

    Thank you Ryiah, I will keep that in mind. that should help me.
     
  9. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    13,872
    That script runs flawlessly on my end. Check the console window and check the cube's Inspector.

    upload_2019-5-21_21-44-14.png
     
    JoeStrout likes this.
  10. sadsack

    sadsack

    Joined:
    May 27, 2015
    Posts:
    124
    I been working a long time I have check both many time. I am to tired to do any more tonight.
    thank you
    I watch both youtube
    Renny
     
  11. sadsack

    sadsack

    Joined:
    May 27, 2015
    Posts:
    124
    By the way I am using unity 3D 5.6
     
  12. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    7,893
    Ryiah's advice is good. And when you post a screen shot, please be sure to post a relevant screen shot. The relevant info in this case is how the script is attached to the cube, and what its properties are, as in Ryiah's screen shot. You should also always include the Console, in case there are any errors. Your screen shot did show the Console, but failed to show the inspector for the cube, so my guess is that your script was not actually attached there.
     
  13. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    13,872
    Yes, I noticed that, but it shouldn't matter for this situation as Unity's UI hasn't really changed in a meaningful way since the initial release, and even if it has Unity isn't going to backport fixes to Unity 5.6 meaning anything broken at this point will be broken for good. But I can't imagine it's actually broken.

    https://bitbucket.org/Unity-Technologies/ui/commits/branch/5.6 - Last commit was May 2017
     
    Last edited: May 22, 2019
  14. sadsack

    sadsack

    Joined:
    May 27, 2015
    Posts:
    124
    Hi All,
    I got it working, I have never use unity UI be for. So I watch about 10 r 12 videos on the subject. Then I turn to unity and deleted all the ui. I redid the ui and then it worked. I want to thank all of you for your great help.

    xyz_it works.png
     
    JoeStrout likes this.
  15. sadsack

    sadsack

    Joined:
    May 27, 2015
    Posts:
    124
    OK, I back working to try to input an xyz into a input field and move My spaceship to that xyz spot.
    Will let you know how things are going.