Numbers not matching up on journey between two points
Hi, sorry for about the nature of the question, I didn't quite know how else to word it. So, here's the issue: I'm experimenting with making a basic train sim (not a fan myself but I kinda made a bet with a friend who likes them that I could make one). I've got a code that's intended to manage the journey between two stations (represented by two station sprites located at different points on the x axis and a train sprite moving between them). I thought everything was going well, until I tried the journey back to station A from station B.
The idea is that the train accelerates at a constant rate up to (but not necessarily reaching) a max speed. During the time of acceleration, every fixed frame it takes the time taken to reach its current speed, uses it to calculate the distance travelled during the acceleration period and compares it with the remaining distance of the journey. This way the code can be applied to any journey length (it's not 100% reliable, so I told it to allow a little extra distance for the time taken to perform the calculation and tried to limit the margin of error). The first journey is fine. It accelerates and then decelerates in time to stop near enough on target. However, on the journey back, it appears to have a longer acceleration period and then decelerates too early. I really don't understand it since it's travelling the same distance on each journey.
I'm not particularly good at maths, and I'm not sure whether it's a coding problem or a mathematical one. If anyone can shed any light on it, I'd really appreciate it. The code is below:
Edit: So, after running various tests, I think I've determined that the problem lies in the acceleration rate on the way back to station A being halved. However, I've no idea why this is.
using UnityEngine;
using System.Collections;
public class TrainMove : MonoBehaviour
{
public GameObject target;
public GameObject location;
public bool launchtrain = false;
public bool accelerate = false;
public bool decelerate = false;
public float trainspeed = 0f;
public float kph = 0f;
public float metres = 0f;
public float acceltime = 0f;
public float velocitychangedistance = 0f;
public string distance;
public Vector2 direction;
void FixedUpdate()
{
kph = trainspeed * 3600f;
metres = Mathf.Abs (transform.position.x - target.transform.position.x) * 1000f;
if (Mathf.Abs (transform.position.x - target.transform.position.x) >= 1f) {
distance = Mathf.Abs (transform.position.x - target.transform.position.x).ToString ("F1") + " km";
}
else
{
distance = metres.ToString ("F0") + " m";
}
if (launchtrain)
{
if (accelerate)
{
trainspeed += 0.0005f * Time.fixedDeltaTime;
if(trainspeed > 0f)
{
acceltime += 1 * Time.fixedDeltaTime;
}
velocitychangedistance = 0.5f * trainspeed * acceltime;
}
if (trainspeed < 0.04f && !decelerate)
{
accelerate = true;
}
else
{
accelerate = false;
}
if (decelerate) {
if (trainspeed > 0f) {
trainspeed -= 0.0005f * Time.fixedDeltaTime;
}
}
if (Mathf.Abs (transform.position.x - target.transform.position.x) - 0.0005 <= velocitychangedistance) {
decelerate = true;
accelerate = false;
}
transform.Translate (direction * trainspeed * Time.fixedDeltaTime);
if (Mathf.Abs (transform.position.x - target.transform.position.x) <= 0.0015f && trainspeed <= 0f) {
print ("Arrived!");
decelerate = false;
launchtrain = false;
}
}
}
void GetDirection()
{
foreach(GameObject gameobject in GameObject.FindObjectsOfType(typeof(GameObject)))
{
if(gameobject.GetComponent<SpriteRenderer>() != null)
{
if(gameobject.GetComponent<SpriteRenderer>().sprite.name == "station")
{
if(Mathf.Abs (transform.position.x - gameobject.transform.position.x) <= 0.0015f)
{
location = gameobject;
}
if(gameobject != location)
{
target = gameobject;
}
}
}
}
if (transform.position.x < target.transform.position.x)
{
direction = Vector2.right;
}
if (transform.position.x > target.transform.position.x)
{
direction = Vector2.left;
}
}
void OnGUI()
{
GUI.Box (new Rect(500 -175f, 500f -50f, 250f, 100f),"Train Journey\nTrain Speed: "+kph.ToString("F1")+" km/h"+"\nDistance To Next Stop: "+distance);
if(GUI.Button (new Rect(500f -50f, 500f +15f, 100f, 25f), "Launch"))
{
launchtrain = true;
GetDirection();
}
}
}
Okay, so I don't know if this will ever be useful to anyone else, but ah well. I found out what was going wrong. $$anonymous$$y code never reset the acceleration time value ("acceltime") for the start of the next journey, so the numbers were all wrong. But it is now fixed and working well!
Your answer
Follow this Question
Related Questions
Cumulative acceleration for copies. 0 Answers
How to Limit or Remove Acceleration with RigidBody2D? 1 Answer
Player object accelerates??? C# Unity2D 0 Answers
Face direction of a Vector 3 1 Answer
How to make 2 different actions for the same input? 2 Answers