Quantcast
Channel: Questions in topic: "loops"
Viewing all articles
Browse latest Browse all 184

do-while loop problems

$
0
0
I have a script to spawn an object in front of the player, and then move the player to a child object ("Target") of the the spawned object ("Straight"). However, my do-while loop messes things up. (I've been testing the code using the if statement where Direction = "-X") When I disable it with // it gets skipped (causing the player to not move) and everything works fine, but when it's enabled, once a press the up arrow, the game freezes, without showing and of the print("blah") things I have before the do-while loop. Any help would be appreciated. Code: using UnityEngine; using System.Collections; public class TrainMover : MonoBehaviour { //only 4 direction movement //use arrow keys //up = forward //down = nothing //left = turn left //right = turn right //apply to train bool moving = false; public GameObject Straight; public GameObject Turn; Vector3 loc; Quaternion rot; public string Direction = "-X"; public GameObject TPC; void Start() { Instantiate(Straight, TPC.transform.position, TPC.transform.rotation); Destroy(GameObject.Find("Target")); } void FixedUpdate() { if (moving == false) { if (Input.GetKeyDown(KeyCode.UpArrow)) { Forward(); print("Forward!"); } if (Input.GetKeyDown(KeyCode.LeftArrow)) { Left(); } if (Input.GetKeyDown(KeyCode.RightArrow)) { Right(); } } } void Forward() { print("Forward! (succesfully called)"); moving = true; if (Direction == "X") { loc = TPC.transform.position; loc.x += 9.6f; rot = gameObject.transform.rotation; Instantiate(Straight, loc, rot); do { gameObject.transform.position += new Vector3(0.01f, 0, 0); } while (gameObject.transform.position != GameObject.Find("Target").transform.position); Destroy(GameObject.Find("Target")); moving = false; } if (Direction == "Z") { loc = TPC.transform.position; loc.z += 9.6f; rot = gameObject.transform.rotation; Instantiate(Straight, loc, rot); do { gameObject.transform.position += new Vector3(0, 0, 0.1f); } while (gameObject.transform.position != GameObject.Find("Target").transform.position); Destroy(GameObject.Find("Target")); moving = false; } if (Direction == "-X") { print("-X"); loc = TPC.transform.position; loc.x += -9.6f; rot = gameObject.transform.rotation; Instantiate(Straight, loc, rot); do { print("Moving"); gameObject.transform.position -= new Vector3(0.01f, 0, 0); } while (gameObject.transform.position != GameObject.Find("Target").transform.position); Destroy(GameObject.Find("Target")); moving = false; print("Finished"); } if (Direction == "-Z") { loc = TPC.transform.position; loc.z += -9.6f; rot = gameObject.transform.rotation; Instantiate(Straight, loc, rot); do { gameObject.transform.position -= new Vector3(0, 0, 0.1f); } while (gameObject.transform.position != GameObject.Find("Target").transform.position); Destroy(GameObject.Find("Target")); moving = false; } } //As you can see, I've been waiting to fix this before moving on void Left() { } void Right() { } }

Viewing all articles
Browse latest Browse all 184

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>