Move items in a direction

Hello! First at all thanks for your time and help.

Let me try explain my problem. I created a very rudimentary script to place items. And now I want to move them.

But that movement isn’t between two points exactly, it is in a direction. For example if the user choose “North” the movement will be to “South”, if he choose “NorthWest” movement will be to “SouthEast”. For those cardinals points I created two empty GameObjects that I place in the “Start” in a particular place:


So the problems are:

  • How to check Direction between those two points.
  • How to move items in a Direction instead to a particular point.

Again, thanks so much

public class Mover : MonoBehaviour {
    public Vector2 direction;
    public float speed = 5;

    void Update() {
        transform.position += direction.normalized * speed * Time.deltaTime;
    }
}
3 Likes

Thanks so much Joe Strout, again you are helping me alot. I was able to continuing trying things thanks to you.

In this case I was able to use the “coordinates” that I did for the direction and to keep my items moving indefinitly.

Now I am trying to calculate distance (between the item starting point and a radius) and destroy it when reach it. Still need alot work, but first I want try it by myself :slight_smile:

Again Thanks!

For checking distance you could use the following.

Update()
{
     // Dest - our position = the vector from us to them.  Then use
     // magnitude to get the 'length' of that vector line.
     float dist = Vector3.Magnitude(target.transform.position - transform.position);

     if(dist >= ourKillLimit)
     Destroy(this.gameObject);
}

You could have this on your moving objects (if I understand your idea properly) and target will be what it’s checking against. ourKillLimit is what ever float you want to set the distance too.

Hope that actually makes sense… I’m trying a new shorter post style…

2 Likes

For that matter Unity’s Vector classes have Distance methods.

Update()
{
     float distance = Vector3.Distance(target.transform.position, transform.position);

     if(dist >= ourKillLimit)
     Destroy(this.gameObject);
}
1 Like

Thanks so much to both, I already finishing the script. Just adding some tweaks :smile:

1 Like