<Scr_UnityChan>
using UnityEngine;
using UnityEngine.UI; // UI系のコンポーネントを使用する為に必要
using UnityEngine.SceneManagement; // SceneManagerを使用する為に必要
using System.Collections;
public class Scr_UnityChan : MonoBehaviour {
Animator animator;
//Rigidbody用の変数 Rigidbody rb; // UnityのHierarchyビューにあるUnityChanから値を直接編集できるようになる public float Speed = 0; public float JumpPower = 0;//ユニティちゃんのジャンプ力 public Text TextScore; // ユニティちゃんからテキストオブジェクトをいじるためpublicにする public AudioClip SeJump; // ユニティちゃんからジャンプ時のSEを設定するためpublic bool jumpStartFlag = false; //ジャンプ開始フラグ bool groundFlag = false; //着地フラグ //1回前のX座標の保存用 float prevPosX = -1; int score = 0; //ゲームのスコア float Itemtimer = 0f; bool useItem = false; //実行された時に1回だけ呼ばれる void Start() { // unitychanのAnimatorコンポーネントをScriptで扱えるようにする animator = GetComponent<Animator>(); // unitychanのRigidbodyコンポーネントをScriptで扱えるようにする rb = GetComponent<Rigidbody>(); //走りモーションに移行 animator.SetBool("IsRunning", true); } //1F(1/60秒)毎に呼ばれる(入力系の処理) void Update() { if (useItem == true) { Itemtimer -= Time.deltaTime; if (Itemtimer <= 0) { useItem = false; Speed = 4; } } } // 1F(1/60秒)毎に呼ばれる(物理演算系の処理) void FixedUpdate() { //ユニティちゃんの現在のY座標が0でない if (transform.position.y == 0 || transform.position.y == 0.6899) { } //ユニティちゃんの現在のY座標が0 else { // 落ちたら if (transform.position.y <= -5) { //ゲームオーバーシーンへ移行 SceneManager.LoadScene("UnityChanRun_GameOver"); } } //jumpStartFlagがtrue? if (jumpStartFlag == true) { //ジャンプモーションに遷移 animator.SetBool("IsJump", true); //上方に速度を加える rb.AddForce(Vector3.up * JumpPower, ForceMode.VelocityChange); jumpStartFlag = false; } //前回のX座標と現在のX座標が同じでないなら if (transform.position.x != prevPosX) { // x軸方向に進むようにする。 ※yとzを0にしないように注意 rb.velocity = new Vector3(Speed, rb.velocity.y, rb.velocity.z); } //X座標の保存 prevPosX = transform.position.x; } //物体と物体が触れたときに呼ばれる void OnCollisionEnter(Collision collision) { //ジャンプモーションのフラグをfalseにする animator.SetBool("IsJump", false); groundFlag = true; } //物体と物体が触れたときに呼ばれる void OnCollisionExit(Collision collision) { groundFlag = false; } //物体と物体が触れたときに呼ばれる(IsTrigger有効時) void OnTriggerEnter(Collider other) { // 衝突したGameObjectのタグが「goal」であるか if (other.tag == "goal") { // IsJumpとIsRunningをfalseにし、 // IsGoalをtrueにする animator.SetBool("IsJump", false); animator.SetBool("IsRunning", false); animator.SetBool("IsGoal", true); // 止まらせる Speed = 0; JumpPower = 0; // UnityChanを00度回転させて、こっちを向かせる transform.Rotate(0, 180, 0); // GameObject Goalを消滅させる Destroy(other.gameObject); } } public void Jump() { if (groundFlag == true) { //マウス左ボタン押したら(Androidの場合はタップ)
// if (Input.GetMouseButtonDown(0) == true)
{
//ジャンプ用のフラグを立てる
jumpStartFlag = true;
// SEを鳴らす GetComponent<AudioSource>().PlayOneShot(SeJump); } } Debug.Log("lllllll"); } //ユニティちゃんのスコア加算用関数 public void AddScore(int value) { //スコアに加算する score = score + value; //Consoleに出力する Debug.Log("Score = " + score); // Score:~を表示 TextScore.text = "Score:" + score.ToString(); } //ユニティちゃんのスピード加算関数 public void SpeedItem(int value) { //スピードを加算する if (useItem == false) { Speed = Speed + value; //Consoleに出力する Debug.Log("Speed = " + score); Itemtimer = 3f; useItem = true; } } //ユニティちゃんのジャンプ加算関数 public void JumpItem(int value) { //ジャンプを加算する if (useItem == false) { Speed = Speed + value; //Consoleに出力する Debug.Log("Jump = " + score); Itemtimer = 3f; useItem = true; } }
}
<Scr_Jump>
using UnityEngine;
using System.Collections;
public class Scr_Jump : MonoBehaviour {
public int Jump = 0; //ジャンプ
//実行された時に1回だけ呼ばれる void Start(){ } //1F(1/60秒)毎に呼ばれる void Update(){ } //物体と物体が触れたときに呼ばれる void OnTriggerEnter(Collider other){ //Componentを取得 Scr_UnityChan unitychan = other.gameObject.GetComponent<Scr_UnityChan>(); // Scr_UnityChanのAddScore呼び出し unitychan.JumpItem(Jump); //アイテムの削除 Destroy(gameObject); }
}
<Scr_RightLeft>
using UnityEngine;
using System.Collections;
public class Scr_RightLeft : MonoBehaviour {
public GameObject unuty;
public Vector3 SPEED = new Vector3(0.05f, 0.05f, 0.05f);
// Use this for initialization
void Start(){
}
// Update is called once per frame void Update() { } public void MoveLeft() { // 現在位置をPositionに代入 Vector3 Position = unuty.transform.position;
// if (Input.GetKey("left"))// 左キーを押し続けていたら
{
// 代入したPositionに対して加算減算を行う
Position.z += SPEED.z;
}
// 現在の位置に加算減算を行ったPositionを代入する
unuty.transform.position = Position;
}
public void MoveRight()
{
// 現在位置をPositionに代入
Vector3 Position = unuty.transform.position;
// if (Input.GetKey("right")) // 右キーを押し続けていたら
{
// 代入したPositionに対して加算減算を行う
Position.z -= SPEED.z;
}
// 現在の位置に加算減算を行ったPositionを代入する
unuty.transform.position = Position;
}
}
この様にスクリプトを組んでみましたが、ボタンを押さないと飛ばず、左右移動は逐一連打しないと移動できません。
・右のボタンを押している間は右に、左のボタンを押している間は左に移動する。
・画面のどこかをタップ、尚且つ地面に接している時のみジャンプを行う。
上記の二つの動作をさせるためにはどのようなスクリプトを組めばいいのでしょうか?