Inverted distance?

Hello,
I was wondering if there is a way to get the inverted distance from a Vector3 position. I have a scenario where I have an object chasing another object. In order to calculate the movement, I use…

//Calculate the movement
Public float speed;

Vector3 move = ((transform.position - playerObj.transform.position) * Time.deltaTime);

//Limit move by speed to where if the Vector3.Distance(new Vewctor3(0, 0, 0), move) is greater than speed, make move equal the speed from new vector3(0, 0, 0), by Vector3 equation.
???

//Move the chasing object
transform.position -= move;

… with my script attached to the chasing object, but what if I would like to limit move, by a floated ‘speed’ variable? Thank you for any suggestions.

Hi, you should first calculate move without multiplying it by deltaTime.
Then movement delta can be calculated with move.normalized * (deltaTime * speed)

True, but it worked the same. I wanted to make sure that the move plus the deltaTime didn’t exceed the speed. Here is what I added after calculating the movement plus the deltaTime…

float excess = Vector3.Distance(new Vector3(0, 0, 0), move) * 1.0f / enemySpeed;
if (Vector3.Distance(new Vector3(0, 0, 0), move) > enemySpeed) {
    move /= excess;
}

… but there should be a ‘InvertDistance’ function (to make things easier).

            Vector3 move = transform.position - playerObj.transform.position;
            move=Vector3.ClampMagnitude(move,10);   // limit speed to 10
            transform.position-=move*Time.deltaTime;
1 Like