- Home /
Question by
taqiarzoo424 · Apr 03, 2020 at 08:32 AM ·
c#errordistancevector3.distancetravel
how to Calculate A 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
Shortest distance between two meshes/colliders 3 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
ArgumentException: JSON ArgumentException: JSON must represent an object type. 0 Answers
Open door = load scene c# issues 4 Answers