Search Unity

How do I make an object go to a certain position in Unity 2D?

Discussion in 'Navigation' started by ArvidPiSa05, Oct 14, 2019.

  1. ArvidPiSa05

    ArvidPiSa05

    Joined:
    Oct 11, 2019
    Posts:
    3
    So here is a simple question, not sure if this is the right place for this but I'm pretty new to Unity and i wonder how i can make an object go to a certain position.

    I have a button, and in its script i have an empty OnMouseDown thing (not sure what it is called). So i want to make the OnMouseDown to move another game object to another position. How do i do this?
    This is in Unity 2D.

    Edit: I would like the game object to glide to the new position.
     
  2. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    Not really related to this forum since this is meant for AI navigation and pathfinding, but I'll help you out.

    UI Buttons can actually do this for you without too much code necessary. If you select your Button and look at the Button component in the Inspector you'll see it has a section called On Click () with an empty list under it.
    If you click the + button beneath it, you add a new element in the list.
    This is a Listener, Listeners "listen" for the button to be clicked and then execute whatever you specify, each time you click the button.

    First, you need to specify which object you want to listen to the button. This will be your moving object.
    Simply drag it into the empty slot.
    Next, you need to write the actual code to move your object.
    To make this as simple as possible, make a new C# script called Moverwith this code:

    Code (CSharp):
    1. public class Mover : MonoBehaviour
    2. {
    3. public float speed = 1f;
    4. private Vector3 _targetPosition;
    5.  
    6. private void Start()
    7. {
    8. _targetPosition = transform.position;
    9. }
    10.  
    11. public void MoveTo(Vector3 position)
    12. {
    13. _targetPosition = position;
    14. }
    15.  
    16. private void Update()
    17. {
    18. transform.position = Vector3.MoveTowards(transform.position, _targetPosition, speed * Time.deltaTime);
    19. }
    20. }
    This code simply sets a target position and moves towards it at the speed value you specify.
    Because speed is set to public, you can change it at any time in the inspector of your object with this code added to it, or through code itself using yourObject.Mover.speed = someValue.
    Technically, this component forces you to move at all times by doing the actual move in Update, but since you initially set the target position to its current position it'll first just move to its current place. That way it looks as if you're starting and stopping your movement.

    Now you just need to add the Mover to your moving object.
    Then, going back to your button you can use the dropdown next to your object in that list to select the Mover component, and select the MoveTo method. You should now get a Vector3 field in that list next to it. This is where you put your destination in.

    I recommend you read up more on basic scripting and Unity's UI tools before continuing after this.
     
  3. ArvidPiSa05

    ArvidPiSa05

    Joined:
    Oct 11, 2019
    Posts:
    3
    Thank you very much for this reply. Just one more question:
    On line 4 in your script it says _targetPosition. Let's say that i want my game object to go to 1x, 1y (and 1z). where do i write this? Right before that?

    Again, thank you for helping me out. Sorry for these questions, but i am as i said new to this. I am also looking it up, i just didn't find anything about just this.
     
    Last edited: Oct 15, 2019
  4. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    You don't need to write that in the code there.
    Instead, when you add the MoveTo method to the button's listener you will get a field for a vector3, you can write your target position (1,1,1) in there. Remember, the button will call the MoveTo method and put that position in place of (targetPosition).
    Again, really read up on coding with Unity and C#, you're going to need to.
     
  5. bwheelerk12

    bwheelerk12

    Joined:
    Aug 5, 2021
    Posts:
    1
    Live Gougar Reactions:eek:
     

    Attached Files: