Skip to content

Instantly share code, notes, and snippets.

@teach310
Created July 1, 2016 05:20
Show Gist options
  • Save teach310/42dc227dfe0d040e2bcde1a4cb21b06b to your computer and use it in GitHub Desktop.
Save teach310/42dc227dfe0d040e2bcde1a4cb21b06b 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;
//状態を定義
public enum PlayerState
{
BLUE,
RED
}
//現在の状態
private PlayerState _currentState;
void Start () {
InitState ();
}
void Update () {
switch (_currentState) {
case PlayerState.BLUE:
UpdateBlue ();
break;
case PlayerState.RED:
UpdateRed ();
break;
default:
break;
}
Debug.Log (_currentState);
}
//状態の初期化
void InitState(){
_currentState = PlayerState.BLUE;
currentObject = blueObject;
}
//状態がBlueのときにやること
void UpdateBlue(){
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.C)) {
_currentState = PlayerState.RED;
ChangeMode (redObject);
}
}
//状態がRedのときにやること
void UpdateRed(){
if (Input.GetKeyDown (KeyCode.C)) {
_currentState = PlayerState.BLUE;
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