void CheckSwitchLane() { if (!isJumping) // cannot switch lane while jumping { if (Input.GetKeyDown(KeyCode.UpArrow)) { // change lanes UP this.currentLane -= 1; if (this.currentLane < lanes.lane1) { this.currentLane = lanes.lane1; } SwitchLanes(currentLane); } else if (Input.GetKeyDown(KeyCode.DownArrow)) { // change lanes DOWN this.currentLane += 1; if (this.currentLane > lanes.lane3) { this.currentLane = lanes.lane3; } SwitchLanes(currentLane); } } }
public void SwitchLanes(lanes l) { Vector3 lanePos = laneObjects[(int)l].transform.position; lanePos.z = -1; // fixed z-axis: always front //lanePos.y += this.GetComponent<SpriteRenderer>().bounds.size.y / 2; //this.transform.position = lanePos; if ((int)l == 0) { this.GetComponent <SpriteRenderer>().sortingOrder = -13; } else if ((int)l == 1) { this.GetComponent <SpriteRenderer>().sortingOrder = -9; } else if ((int)l == 2) { this.GetComponent <SpriteRenderer>().sortingOrder = -5; } isSwitchPlane = true; time = 0; startPosition = this.transform.position; target = lanePos; // turn off others lane's collider triggerColliders(l); }
public void PrintDirection(string direction) { Debug.LogError(direction); if (!GameController.instance.isGameOver) { if (!isJumping) { if (direction.Contains("Up")) { this.currentLane -= 1; if (this.currentLane < lanes.lane1) { this.currentLane = lanes.lane1; } SwitchLanes(currentLane); } else if (direction.Contains("Down")) { this.currentLane += 1; if (this.currentLane > lanes.lane3) { this.currentLane = lanes.lane3; } SwitchLanes(currentLane); } } } }
// Use this for initialization void Start() { position = transform.position; isAlive = true; Time.timeScale = 1f; playerScore = 0; animator = GetComponent <Animator>(); endPos = transform.position; lane = lanes.lane2; source = GetComponent <AudioSource> (); }
void triggerColliders(lanes l) { for (int i = 0; i < laneObjects.Length; i++) { if ((int)l == i) { laneObjects[i].GetComponent <BoxCollider2D>().enabled = true; } else { laneObjects[i].GetComponent <BoxCollider2D>().enabled = false; } } }
// Use this for initialization void Start() { GameControllerScript = GameObject.Find("GameScripts").GetComponent <GameController>(); playerSpeed = GameControllerScript.playerSpeed; playSpawnAnimation = true; if (transform.position.x == 0) { currentLane = lanes.middle; } if (transform.position.x < 0) //right { currentLane = lanes.right; } if (transform.position.x > 0) { currentLane = lanes.left; } }
// Update is called once per frame void Update() { switch (Input.inputString) { case "a": if (currentLane == lanes.middle) { currentLane = lanes.left; transform.position = new Vector3(35, 7.5f, 4970); } else if (currentLane == lanes.right) { currentLane = lanes.middle; transform.position = new Vector3(0, 7.5f, 4970); } break; case "d": if (currentLane == lanes.left) { currentLane = lanes.middle; this.transform.position = new Vector3(0, 7.5f, 4970); } else if (currentLane == lanes.middle) { currentLane = lanes.right; transform.position = new Vector3(-35, 7.5f, 4970); } break; case "w": GameControllerScript.playerSpeed += 100; break; default: break; } }
// Use this for initialization void Start() { currentLane = lanes.middle; GameControllerScript = GameObject.Find("GameScripts").GetComponent <GameController>(); UIhandlerScript = GameObject.Find("UI").GetComponent <UIhandler>(); }
// Update is called once per frame void Update() { transform.position = position; if (lane == lanes.lane1) { position = new Vector3(position.x, 5, position.z); } if (lane == lanes.lane2) { position = new Vector3(position.x, 0, position.z); } if (lane == lanes.lane3) { position = new Vector3(position.x, -5, position.z); } if (Input.GetKeyDown(KeyCode.UpArrow)) { switch (lane) { case lanes.lane1: break; case lanes.lane2: lane = lanes.lane1; break; case lanes.lane3: lane = lanes.lane2; break; default: lane = lanes.lane2; break; } } if (Input.GetKeyDown(KeyCode.DownArrow)) { switch (lane) { case lanes.lane1: lane = lanes.lane2; break; case lanes.lane2: lane = lanes.lane3; break; case lanes.lane3: break; default: lane = lanes.lane2; break; } } if (Input.GetKey(KeyCode.RightArrow) && position.x < 23) { position = new Vector3(position.x + 15 * Time.deltaTime, position.y, position.z); } if (Input.GetKey(KeyCode.LeftArrow) && position.x > -5) { position = new Vector3(position.x - 15 * Time.deltaTime, position.y, position.z); } // Running sound if (Time.time > runningTimer - 2.3f) { runningTimer = Time.time + runningSound.length; source.PlayOneShot(runningSound); } // traffic sound if (Time.time > trafficTimer) { trafficTimer = Time.time + trafficSound.length; source.PlayOneShot(trafficSound); } // BGM sound if (Time.time > bgmTimer) { bgmTimer = Time.time + bgm.length; source.PlayOneShot(bgm); } //Jumping if (Input.GetKeyDown(KeyCode.Space) && !invincible) { //invincible = true; animator.SetTrigger("isJumping"); timer = 0.0f; endPos = new Vector3(position.x + 6, position.y, position.z); } if (timer <= 1) { timer += Time.deltaTime / 0.75f; var height = Mathf.Sin(Mathf.PI * timer) * 4f; position = Vector3.Lerp(position, endPos, timer) + Vector3.up * height; if (Input.GetKeyDown(KeyCode.UpArrow) && position.y < 5) { endPos = new Vector3(position.x, position.y + 5 * Time.deltaTime, position.z); } if (Input.GetKeyDown(KeyCode.DownArrow) && position.y > -5) { endPos = new Vector3(position.x, position.y - 5 * Time.deltaTime, position.z); } //if (Input.GetKey(KeyCode.RightArrow) && position.x < 23) //{ // endPos = new Vector3(position.x + 15 * Time.deltaTime, position.y - height, position.z); //} //if (Input.GetKey(KeyCode.LeftArrow) && position.x > -5) //{ // endPos = new Vector3(position.x - 15 * Time.deltaTime, position.y - height, position.z); //} } //Reset the speed and difficulty for if they restart; if (!isAlive) { EnemyVehicle.speed = 10; EnemySpawn.increaseDifficulty = 1; //EnemySpawn.harder = false; } }