UNITY3D How to make an object stay at a certain distance from another object?

0

I have two ships in UNITY, let's call them A and B:

Ship A moves steadily forward.

Ship B has to chase ship A, approaching it and rotating around it but at a certain distance.

The basic skeleton of the operation is the following:

private void()
{
    Move();
    Turn();
}

For B to chase A I use this code:

private void Move()
{
    transform.position += transform.forward * movementSpeed * Time.deltaTime + target.transform.position;
}

That is in a method called Move which I invoke in Update. I also have another method called Turn that handles rotations:

private void Turn()
{
    Vector3 pos = target.transform.position - transform.position;
    Quaternion rotation = Quaternion.LookRotation(pos);
    transform.rotation = Quaternion.Slerp(transform.rotation, rotation, rotationalDamp * Time.deltaTime);
}

How can I do, so that from a certain distance, ship B did not get closer to ship A and keep rotating (like orbiting) around her?

Thank you very much!

    
asked by OnlyDavies 20.07.2018 в 08:18
source

1 answer

0

You condition it. Example

if(pos <= 0.5f)
 {
   //Rotar en su orbita
 }
else if (pos > 0.5f)
 {
   //Seguir a nave A
 }

This way you will not follow it if it is in its minimum distance, and instead of following it will proceed to rotate.

    
answered by 28.09.2018 в 07:22