Skip to content

Instantly share code, notes, and snippets.

@teach310
Created July 1, 2016 05:19
Show Gist options
  • Save teach310/76e4ab6e20d765aaf77cbfd73cea7671 to your computer and use it in GitHub Desktop.
Save teach310/76e4ab6e20d765aaf77cbfd73cea7671 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
public class PlayerScript : MonoBehaviour {
public GameObject blueObject;
public GameObject redObject;
private GameObject currentObject;
int currentState = 0; // 0: Blue 1: Red
void Start () {
currentObject = blueObject;
}
void Update () {
// state: Blue
if (currentState == 0) {
Debug.Log ("Blue");
// 移動
if (Input.GetKey (KeyCode.RightArrow)) {
this.transform.position += Vector3.right * Time.deltaTime * 5f;
}
if (Input.GetKey (KeyCode.LeftArrow)) {
this.transform.position += Vector3.right * (-1) * Time.deltaTime * 5f;
}
// 状態変更
if (Input.GetKeyDown (KeyCode.R)) {
currentState = 1;
ChangeMode (redObject);
}
}
// state: Red
if (currentState == 1) {
Debug.Log ("Red");
if(Input.GetKeyDown(KeyCode.B)){
currentState = 0;
ChangeMode (blueObject);
}
}
}
void ChangeMode(GameObject obj){
currentObject.SetActive(false);
currentObject = obj;
currentObject.SetActive (true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment