M
M
marsep2015-11-30 01:34:08
Unity
marsep, 2015-11-30 01:34:08

How to make an object dynamically move after another in 2D (Unity)?

Hello. I have already spent a lot of time trying to solve the following problem: there are two objects, A and B, object A is in position Y = 0, and object B is in position Y = -100. Object A is the leader and is in constant motion, for example:
transform.position += transform.up * speed;
Object B must move behind it so that if the distance between them, for example, is above 300, then object B adds speed to the base and gradually catches up with object A, and if the distance is lower, then object B, on the contrary, lags behind object A, reducing the base speed. At the start, the base speed of object B is the same as that of object A. The distance can be calculated as follows:

var distance = (a.transform.position - b.transform.position).magnitude;

The distance is calculated as intended, but the problem is this - I can't make object B catch up and lag behind object A. I've tried many ways already. I tried using simple conditions, where at some moments the speed was reduced or added to object B, but the problem I had was that object B was constantly twitching, i.e. Under the terms, it doesn't work well. I execute accordingly in the Update method. Tell me how you can solve such a thing so that object B can lag behind or catch up with object A while doing it correctly and when it is really necessary in terms of distance, and so that there are no such problems, such as twitching, etc. Many thanks in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
marsep, 2015-12-02
@marsep

Due to little experience, I did not know about the interesting Lerp method in Vector3:
Vector3.Lerp

V
Vladimir Limarchenko, 2015-11-30
@vovo801

You can first try a couple of ready-made scripts like Smooth Camera Follow.
For example:
answers.unity3d.com/questions/29183/2d-camera-smoo...
Easy to use. The script is hung on what should follow the other thing, and a link to what should be followed is placed in the target field in the Inspector.
It's best not to move an object that already has a Rigidbody in this way. Then there will be vibration.
To keep the distance, you can insert a simple if statement into the code from the example

Vector3 delta = target.position - camera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, point.z));
if (delta. magnitude > someValue)
{
        Vector3 destination = transform.position + delta;
        transform.position = Vector3.SmoothDamp(transform.position, destination, ref velocity, dampTime);
}

UPDATE: I read it again - I wanted to write about the backlog. You can play around with dampTime -
depending on this value, the object will follow the target smoothly or quickly. You can set it so that there is a smooth movement, that is, it will catch up if you wait a long time, but turn on the code -
Vector3 delta = target.position - camera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, point.z));
if (delta. magnitude < someValue)
{
        Vector3 destination = transform.position + delta;
        transform.position = Vector3.SmoothDamp(transform.position, destination, ref velocity, dampTime);
}

The difference with what I wrote earlier is only in the sign, not > somevalue, a < somevalue, that is, follow only if the object is close. Then it will be possible to run away from him and he will stop following.
The first option is when > someValue is suitable for the companions that will follow, but it is not necessary that they "climb" the character. And the second - for the enemies - will be pursued until we run away from them to a safe distance. It is only necessary to declare at the beginning of the class:
In general, it is better to make a range - between two values.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question