# Calculating speed between two objects is not working when one of the objects is moving very fast. How can be solved?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;

public class SpaceshipCutscene : MonoBehaviour
{
public Transform player;
public Transform[] npcs;
public Transform console;
public Camera FPSCamera;
public Camera mainCamera;
public Animator[] anim;
public float rotationSpeed = 3f;
public float distanceFromConsole;

private bool moveNpc = false;
private float sp = 0f;
private float distance;

// Use this for initialization
void Start()
{

}

private void Update()
{
distance = Vector3.Distance(transform.position, npcs[0].transform.position);
if (moveNpc)
{
// Soldier 2 rotating and looking at player
Vector3 dir = player.position - npcs[0].position;
dir.y = 0; // keep the direction strictly horizontal
Quaternion rot = Quaternion.LookRotation(dir);
// slerp to the desired rotation over time
npcs[0].rotation = Quaternion.Slerp(npcs[0].rotation, rot, rotationSpeed * Time.deltaTime);

var dist = Vector3.Distance(npcs[1].position, console.position);
if (dist < distanceFromConsole)
{
sp += Time.deltaTime;
sp = Mathf.Clamp(sp, 0f, 1f);
anim[1].SetFloat("WalkingSpeed", sp);
}

Vector3 dirToComputer = console.transform.position - npcs[1].position;
dirToComputer.y = 0;
Quaternion rot1 = Quaternion.LookRotation(dirToComputer);
npcs[1].rotation = Quaternion.Slerp(npcs[1].rotation, rot1, rotationSpeed * Time.deltaTime);

}
}

private void OnTriggerExit(Collider other)
{
if (HoriDoorManager.doorLockState == false && distance < 5f)
{
if (other.gameObject.tag == "SpaceshipCutscene")
{
FPSCamera.enabled = false;
mainCamera.enabled = true;
moveNpc = true;
anim[0].SetBool("Aiming", true);
anim[1].SetBool("Walktouse", true);
}
}
}
}


When moving the player(transform) too fast it will not pass inside the line:

if (HoriDoorManager.doorLockState == false && distance < 5f)


But if I'm moving the player slow very slow or much slower then it will pass inside the line and make the rest of the code in the Update.

Before using the distance at all it was working fine but now I want to calculate the distance to make sure it will work only when the player is exit at specific direction.

But for some reason on high speed movement the distance calculation is not working good. It's not giving errors or exceptions it's just does nothing when exiting.

I did a test now and on the line:

if (HoriDoorManager.doorLockState == false && distance < 5f)


And changed it to:

if (HoriDoorManager.doorLockState == false)


And now it's working on any speed the character/player is moving. So the problem is when calculating the distance on high speed.

• Does (transform.position - npcs[0].transform.position).magnitude work? – Ryolu Oct 23 '18 at 5:42

The distance is just a plain math, you put values in it and get the result but then you check if it's less than 5, and what if it's not? You end up with a situation where in one frame your object is way before some trigger, but in the next frame your object has moved so far that it passed the trigger and is still far away, but on the different side. This is a well known problem in physics with fast moving objects and it's called tunelling.