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,
So this script is not self contained and references stuff outside of the script so i’m unable to get a full picture. So what is your original problem? Is this a car that the player controls or is it a car that moves on it’s own from one point to the next?
Output at beginning of level 2
Here also distance have to be zero at beginning but the distance is 83
This is the actual problem
On level 3 it give same problem
It’s a car from unity standard assets, the is move by standard assets car controller script
Here the reference of calculate distance script.
Each node under spline is marker I use markers to check the car is moving forward or backward.
Index variable store the next marker index
Based on level no another script will enable or disable appropriate start level marker end level marker and obstical.
Output at beginning of level one
Distance is zero and it calculate fine.
Output at beginning of level 2
Here also distance have to be zero at beginning but the distance is 83
This is the actual problem
On level 3 it give same problem
and from the pictures it looks like the script is on the car. Therefore when you go to level two, this start function is not being called again. (Assuming you are teleporting the car and not making a new one.) Now this could not be the problem but it’s hard to know what’s going wrong because there is so much going on and i’m not able to put Debug.Log statements in and poke around to see what’s going on. So I’d suggest you just start exposing the information of your game. Debug statements, break points, and also you can see your private variables if you put the inspector in debug mode (dots to the right of the inspector lock). At some point while you expose more information you’ll notice a variable at the wrong value or that a function in your game is never being called or only being called once. That gives you a point to start investigating why it’s at that wrong value or never being called.
Thank you sir now the problem is fixed
I just move
totalDistance = Vector3.Distance(transform.position, marker.transform.GetChild(index).position);
This line from start to update using if I make it sure that the statement is call only once
On the note of calling it once i’d suggest making an event system in the game. You can achieve this with delegates (which I prefer to use these days) or you can use scriptable objects to make the event system (what i use sparingly these days). That way you can have an event like OnLevelStart. Then have that distance script have a function like RecalculateTotalDistance() that gets triggered by that event. Below is a video on one of the ways to setup an event system but be careful since this can lead to a little bit of editor spaghetti code so use it sparingly.