- Home /
Question by
taqiarzoo424 · Apr 01, 2020 at 10:58 PM ·
unity 5errorvector3.distance
how To Calculate Car Travel Distance towards Target
im trying to Calculate A Car Travel Distance if We move Towards Target The Distance Should Increment If i Move Against The Target then Distance Should Detriment,
int index = GlobalVarible.startPosition[GlobalVarible.levelNo];
void Start()
{
totalDistance = Vector3.Distance(transform.position, marker.transform.GetChild(index).position);
}
void FixedUpdate()
{
distance();
functioncall = false;
}
public void distance()
{
lastDistance = travalDistance;
temp = Vector3.Distance(transform.position,
marker.transform.GetChild(index+1).position);
travalDistance = totalDistance -temp;
distanceText.GetComponent<TextMeshProUGUI>().text = "Distance: " + (travalDistance/3).ToString("0")+"M";
}
void OnTriggerEnter(Collider collision)
{
if (collision.tag == "Marker")
{
markerName = collision.name;
//to Stop Calling Multiple Times on Singe Trigger
if (markerName != markerLastName)
{
if (functioncall) return;
functioncall = true;
if (index >= 0 && index <= marker.transform.childCount)
{
if (lastDistance <= travalDistance)
{
//if car Moving Forward Increment
index = index + 1;
totalDistance += Vector3.Distance(marker.transform.GetChild(index).position, marker.transform.GetChild(index + 1).position);
}
else
{
//if car Moving Backword decrement
index = index - 1;
totalDistance -=
Vector3.Distance(marker.transform.GetChild(index).position,
marker.transform.GetChild(index + 1).position);
}
}
markerLastName = markerName;
}
}
}
the Above Code Work Fine If The Car Start From Marker zero but if the Car Start from Somewhere in between then it will not work Properly,
kindly Help me im very new to Unity
Comment
Your answer
Follow this Question
Related Questions
My player figure falls down after load 1 Answer
Legacy Animations and Unity 5 - What's The Deal? 1 Answer
Unity 5 fatal error 3 Answers
Unity 5 crash on startup 1 Answer
EXC_BAD_ACCESS (SIGABRT) from Unity::Material::CreateMaterial 0 Answers