実現したいこと
Unityで電車を運転するシミュレーターを作っています。
このプログラムで、停止時、ブレーキを緩める際に、何故か少し加速してしまいます。
回答お願いしますm(_ _)m
該当のソースコード
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5 6public class TrainControler : MonoBehaviour 7{ 8 public static float speed = 0; 9 int brake = 5; 10 int notch = 0; 11 bool forward = true; 12 public static double teishi; 13 float Tposx; 14 float Timeleft = 0; 15 float Kari; 16 private float timeleft = -2; 17 public Text speedText; 18 public Text TeisiText; 19 public Text TimeText; 20 public Text notchText; 21 public Text brakeText; 22 int hour = 8; 23 int minute = 59; 24 int second = 35; 25 void Start() 26 { 27 28 } 29 30 void Update() 31 { 32 timeleft -= Time.deltaTime; 33 Timeleft += Time.deltaTime; 34 if (timeleft <= 0.0) 35 { 36 timeleft = 1.0f; 37 second++; 38 if (second > 59) 39 { 40 second -= 60; 41 minute++; 42 } 43 if (minute > 59) 44 { 45 minute -= 60; 46 hour++; 47 } 48 } 49 speedText.text = "Speed:"+(int)(speed/25)+"km/h"; 50 TeisiText.text = "Distance:"+teishi*3+"m"; 51 TimeText.text = "Time"+hour+":"+minute+"." + second; 52 notchText.text = "Notch:" + notch; 53 brakeText.text = "Brake:" + brake; 54 Tposx = transform.position.x; 55 teishi = 740 - Tposx; 56 if(teishi < -1) 57 { 58 teishi += 800; 59 } 60 if (Input.GetKey(KeyCode.Q) ) 61 { 62 if(brake < 8 && notch == 0) 63 { 64 brake++; 65 66 } 67 if(notch > 0) 68 { 69 notch--; 70 } 71 }else if (Input.GetKey(KeyCode.A)) 72 { 73 if(notch > 0) 74 { 75 notch--; 76 } 77 if (brake > 0) 78 { 79 brake--; 80 } 81 } 82 else if(Input.GetKey(KeyCode.Z)) 83 { 84 if (notch < 5 && brake == 0) 85 { 86 notch++; 87 } 88 if (brake > 0) 89 { 90 brake--; 91 } 92 } 93 if (brake == 8 && speed > 0) 94 { 95 speed -= 10 * Time.deltaTime * 30; 96 } 97 if(brake < 8 && brake >0 && speed >0) 98 { 99 speed -= (float)(0.8 * brake) * Time.deltaTime * 30; 100 } 101 if (speed < 0) 102 { 103 speed = 0; 104 } 105 if (brake == 0 && notch == 0 && speed > 0) 106 { 107 speed -= (float)0.1 * Time.deltaTime * 22; 108 } 109 110 if(notch > 0); 111 { 112 if (((float)(notch) - (speed / 640)) > -0.06) 113 { 114 speed += ((float)(notch)- (speed / 640)) * Time.deltaTime * 19; 115 } 116 else 117 { 118 speed -= (float)0.07 * Time.deltaTime * 20; 119 } 120 } if (Input.GetKeyDown(KeyCode.UpArrow) && speed == 0) 121 { 122 forward = true; 123 } 124 if (Input.GetKeyDown(KeyCode.DownArrow) && speed == 0) 125 { 126 forward = false; 127 } 128 Vector3 pos = this.transform.position; 129 if (forward == true) 130 { 131 pos.x += (float)(0.007 * speed * Time.deltaTime); 132 } 133 if (forward == false) 134 { 135 pos.x -= (float)(0.007 * speed * Time.deltaTime); 136 } 137 this.transform.position = pos; 138 } 139} 140
あなたの回答
tips
プレビュー