Please write the code, I will be grateful, I have been suffering for 2 days, it does not work, it doesn’t work, I need your clean code, it will not be big.

Here is my code:

Result 0

using UnityEngine; using System.Collections; public class Atf : MonoBehaviour { public Transform target; public float adrd; public float adrw; void Update () { adrd = Vector3.Distance(transform.position,target.position); if (adrd<30) { adrw +=1 } 

    2 answers 2

    The first thing that came to mind is to introduce a constant with a minimum distance.

     float max = 1000; 

    Then the formula would look like this:

      adrd = Vector3.Distance(transform.position,target.position); adrw = max - adrd; 

      Initialize the member variable for the initial distance (for example, in the constructor):

       //где-то в конструкции или функции init() (и т.п., там, где становитися известно первоначальное растояние между объектами) init_adrd = Vector3.Distance(transform.position,target.position); 

      in the Update () method we calculate:

       adrd = Vector3.Distance(transform.position,target.position); adrw = init_adrd - adrd;