Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Zyrac · Aug 16, 2016 at 06:05 PM · c#movementacceleration

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();
         }        
     }
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Zyrac · Aug 17, 2016 at 01:00 AM 0
Share

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!

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

208 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges