Joe Marks Joe Marks - 2 months ago 16
C# Question

Unity - Make something move but stop when it reaches a certain position?

So here is my code: (Im new and I know if this is incorrect!) BTW this is for Unity

void Update () {
if (Input.GetButtonDown("Left"))
{
Transform.position xPrevPos yPrevPos
rb.velocity = new Vector2(horzMove, 0) * speed;
if(Transform.position == x + 5f)
{
## DO SOMETHING ##
}
}
}


So what I want it to do is to first check if the left button is pressed, then remember the current position, then start the movement, then if it reaches the position I want it stops and listens for another button to be pressed. The reason why I write x + 5f is because I want it to move that distance to the left before it stops

Hope you understand!

Answer Source

This script is taken from one of the tutorials in the official Unity website. It´s a spaceship controller. You set some boundaries in the inspector, so the player can´t move the spaceship beyond those boundaries

using UnityEngine;
using System.Collections;

[System.Serializable]
public class Boundary
{
    public float xMin, xMax, zMin, zMax;
}

public class PlayerController : MonoBehaviour
{
    //public variables you can modify from the inspector
    public float speed;
    public float tilt;
    public Boundary boundary;

    void FixedUpdate ()
    {
        //To read the inputs of the player. The Horizontal and Vertical axis
        //Are defined in the Editor, Settings Manager, Input Manager.
        //You can change that for your own inputs like:
        //Input.GetKeyDown(KeyCode.Enter) or key you want to use
        float moveHorizontal = Input.GetAxis ("Horizontal");
        float moveVertical = Input.GetAxis ("Vertical");

        //Creates a VEctor3D with the inputs and applies the movement
        Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
        rigidbody.velocity = movement * speed;

        //This part keeps the spaceship inside the boundary you passed as parameter
        rigidbody.position = new Vector3 
        (
            Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax), 
            0.0f, 
            Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
        );

        //This part is just to make the spaceship tilt to one side or 
        //another to make it more realistic
        rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
    }
}

And if you are totally new, I would recommend you to start watching all or at least some of theses tutorials before start developing your own ideas. It will make everything easier for you.

https://unity3d.com/es/earn/tutorials/projects/space-shooter/moving-the-player?playlist=17147

And the tutorials: https://unity3d.com/es/learn/tutorials

Edit: To your question why do they use Vector3 in a 2D Game:

Basically even if you are developing a 2D game, the Engine has a 3 dimensional space, so if you define a Vector2 it will be casted to a Vector3 (then giving a 0 value to the third dimension). So in the tutorial they define a Vector3 which will have a 0 value in the y coordinate.