Skip to content

Instantly share code, notes, and snippets.

@ayutaz
Created December 24, 2019 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayutaz/5ef6d2c2ab6d1e6abef71898c1383757 to your computer and use it in GitHub Desktop.
Save ayutaz/5ef6d2c2ab6d1e6abef71898c1383757 to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
public class PlayerController : MonoBehaviour {
    
    public float speed = 4f; //歩くスピード
public float jumpPower = 400; //ジャンプ力
    public LayerMask groundLayer; //Linecastで判定するLayer
    private Rigidbody2D rb2d;
    private bool direction;//キャラクターの向きを保存する true:右,false:左
private bool isGrounded; //着地判定
    private Animator anim;
    // クリアをしたかどうか
    private static bool clear1,clear2;
    private AudioSource sound01;
    private AudioSource sound02;
    private AudioSource sound03;
    // そのクリアに接触しているかどうか,クリア音を慣らすためのフラグ管理
    private bool isnotClear;
    private bool jump;
    private bool push;
    void Start () {
        isnotClear = true;
        direction = true;
        jump = false;
        push = false;
        //各コンポーネントをキャッシュしておく
        anim = GetComponent<Animator>();
        rb2d = GetComponent<Rigidbody2D>();
        AudioSource[] audioSources = GetComponents<AudioSource>();
        sound01 = audioSources[0];
        sound02 = audioSources[1];
        sound03 = audioSources[2];
    }
    void Update(){
        if(jump == true){
            //Linecastでユニティちゃんの足元に地面があるか判定
            isGrounded = Physics2D.Linecast (transform.position + transform.up *
                         1,transform.position -
                          transform.up * 0.7f,groundLayer);
            //スペースキーを押し、
                //着地していた時、
            if (isGrounded) {
                Debug.Log("Jumpを呼び出した");
                //Dashアニメーションを止めて、Jumpアニメーションを実行
                anim.SetBool("Dash", false);
                anim.SetTrigger("Jump");
                //着地判定をfalse
                isGrounded = false;
                //AddForceにて上方向へ力を加える
                rb2d.AddForce (Vector2.up * jumpPower);
                sound01.PlayOneShot(sound01.clip);
            }
            //上下への移動速度を取得
            float velY = rb2d.velocity.y;
            //移動速度が0.1より大きければ上昇
            bool isJumping = velY > 0.1f ? true:false;
            //移動速度が-0.1より小さければ下降
            bool isFalling = velY < -0.1f ? true:false;
            //結果をアニメータービューの変数へ反映する
            anim.SetBool("isJumping",isJumping);
            anim.SetBool("isFalling",isFalling);
            jump = false;
        }
        else if(push == true && direction == true ) RightMove();
        else if(push == true && direction == false) LeftMove();
        else stop();
    }
    void OnCollisionEnter2D(Collision2D other){
        if (other.gameObject.tag == "GameOver"){
            sound02.PlayOneShot(sound02.clip);
            Invoke("GameOver",0.13f);
        }else if (other.gameObject.tag == "stage1"){
            SceneManager.LoadScene("stage1");
        }else if (other.gameObject.tag == "stage2"){
            SceneManager.LoadScene("stage2");
        }else if(other.gameObject.tag == "gameclear"){
            isnotClear = false;
            if(SceneManager.GetActiveScene().name == "stage1"){
                sound01.PlayOneShot(sound03.clip);
                clear1 = true;
                Invoke("next3",3f);
            }else if(SceneManager.GetActiveScene().name == "stage2"){
                sound01.PlayOneShot(sound03.clip);
                clear2 = true;
                Invoke("next3",3f);
            }
            if(clear1 && clear2){
                sound01.PlayOneShot(sound03.clip);
                Debug.Log("クリア画面に移動");
                clear1 = clear2 = false;
                Invoke("GoClear",3f);
            }
        }
}
    void GameOver(){
        SceneManager.LoadScene("GameOver");
    }
    void GoClear(){
        SceneManager.LoadScene("GameClear");
    }
    void next3(){
        SceneManager.LoadScene("Stages");
        isnotClear = true;
    }
    // ここからandroid用に追加
    public void pushDownRight(){
        if(direction == false){
            // ここからキャラ画像の反転をする
            Vector2 temp = gameObject.transform.localScale;
            temp.x *=-1;
            gameObject.transform.localScale = temp;
            direction = true;
            // ここまで
        }
        push = true;
        direction = true;
    }
    public void pushDownLeft(){
        if(direction == true){
            // ここからキャラ画像の反転をする
            Vector2 temp = gameObject.transform.localScale;
            temp.x *=-1;
            gameObject.transform.localScale = temp;
            direction = false;
            // ここまで
        }
        push = true;
        direction = false;
    }
    public void pushUp(){
        push = false;
    }
    void stop(){
        rb2d.velocity = new Vector2 (0, rb2d.velocity.y);
        anim.SetBool ("Dash", false);
    }
    void RightMove(){
        Debug.Log("右に走ってる");
        if(isnotClear){
            rb2d.velocity = new Vector2 (speed,rb2d.velocity.y);
            anim.SetBool ("Dash", true);
        }
    }
    void LeftMove(){
        Debug.Log("左に走ってる");
        if(isnotClear){
            rb2d.velocity = new Vector2 ( speed * -1.0f, rb2d.velocity.y);
            anim.SetBool ("Dash", true);
        }
    }
    public void Jump(){
        jump = true;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment