2020年9月22日火曜日

2Dアクションっぽいスクリプト

自分用のメモとしておいておきます。


「コケモドキと魔女」で使用したプレイヤーのスクリプトです。

2段ジャンプ可能で、Groundタグに触れるとジャンプOKフラグが立ちます。画面外でしぼうすると固定座標で復帰します。左右の向きによって画像を変えています。

先日PCがフリーズしたと思ったらブルースクリーンなったので、バックアップの意味も兼ねています。2Dアクションなどで使いたい方はどうぞ参考にしてください。



using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//using DG.Tweening;
public class fish_corpse : MonoBehaviour
{
    // プレイヤーのすくリプt
    // こけにふれまくってこけをふやしまくる
    // 画面外にでるとしぬ

    int move = 1; // がめおべらで0になってストップ
    public float speed = 8; // 移動はやさ
    Rigidbody2D rb2d;
    public GameObject explosionPrefab; // 爆発エフェクト
    public GameObject explosionPrefab2; // 復活エフェクト
    int jump_point = 1; // ジャンプポイント、1以上でジャンプ可能
    public float flap = 300f; // jumpぱわー
    //float sayuu = 1; // 1だと右移動、-1だと左移動
    float direction = 0f; // キー入力用
    public bool jump = true;
    float mae_y; // y座標保存用
    float direction2 = 0f; // スマホ入力用
    public Sprite Rup;
    public Sprite Rdown;
    public Sprite Lup;
    public Sprite Ldown;
    //RectTransform rectTran; // どついーんテンプレ
    AudioSource audioSource;
    public AudioClip SE_sibou01;

    // Start is called before the first frame update
    void Start()
    {
        Vector3 tmp = this.transform.position;
        rb2d = GetComponent<Rigidbody2D>();
        Instantiate(explosionPrefab2, transform.position, Quaternion.identity); // 復活
        audioSource = GetComponent<AudioSource>();
    }
    // Update is called once per frame
    void Update()
    {

        if (Input.GetKey(KeyCode.RightArrow)) // 右移動
        {
            direction = 1f;
            GetComponent<SpriteRenderer>().sprite = Rup;// 右上
        }
        else if (Input.GetKey(KeyCode.LeftArrow)) // 左移動
        {
            direction = -1f;
            GetComponent<SpriteRenderer>().sprite = Lup;// 左上
        }
        else
        {
            direction = 0f;
        }

        if (Input.GetKeyDown(KeyCode.Space))
        {
            if (jump_point >= 1)
            {
                // GetComponent<AudioSource>().PlayOneShot(SE_jump1); // ジャンプ音再生
                // GetComponent<AudioSource>().PlayOneShot(SE_jump2); // ジャンプ音再生            
                rb2d.velocity = Vector2.zero; // いちど加速度をゼロにする
                rb2d.AddForce(Vector2.up * flap);
                jump_point -= 1;

            }
        }
        rb2d = GetComponent<Rigidbody2D>();
        Vector3 tmp = this.transform.position; // tmpという座標は現在の座標ですよ
        // 落下速度アップ
        if (mae_y > tmp.y) // 直前の座標より今の座標が小さい時(落下中のとき)
        {
            Physics2D.gravity = new Vector2(0.0f, -25.0f);
        }
        else // ジャンプ中&横移動&静止中のとき
        {
            Physics2D.gravity = new Vector2(0.0f, -18.0f);
        }
        mae_y = tmp.y; // 1フレーム前の座標をmae_xに保存
    }

    void FixedUpdate()
    {
        if (move == 1)
        {
            // 右・左.
            //float x = Input.GetAxisRaw("Horizontal");
            /*
            // 上・下.
            float y = Input.GetAxisRaw("Vertical");
            // 移動する向きを求める.
            Vector2 direction = new Vector2(x, y).normalized;
            // 移動する向きとスピードを代入する.
            GetComponent<Rigidbody2D>().velocity = direction * speed;
            */
            //rb2d.velocity = new Vector2((speed * x), rb2d.velocity.y); // 左右移動         
            rb2d.velocity = new Vector2((speed * direction) + (speed * direction2), rb2d.velocity.y); // X軸:キー
            // directionはキーボードの入力、direction2はButtonの入力(スマホ用)
        }

    }
    /*
    //void OnTriggerExit2D(Collider2D coll)
    void OnCollisionExit2D(Collision2D other)
    {
        if (other.gameObject.tag == "Ground")
        {
            jump = false;
            jump_point = 1;
        }
    }
    // void OnTriggerStay2D(Collider2D coll)
    void OnCollisionStay2D(Collision2D other)
    {
        if (other.gameObject.tag == "Ground")
        {
            jump = true;
            jump_point = 2;
        }
    }*/

    //void OnTriggerEnter2D(Collider2D coll)
    void OnCollisionEnter2D(Collision2D other)
    {
        if (other.gameObject.tag == "Ground")
        {
            jump = true;
            jump_point = 2;
        }
    }
    void OnTriggerEnter2D(Collider2D coll)
    // void OnCollisionEnter2D(Collision2D other)
    {
        if (coll.gameObject.tag == "player_wall") // 画面外に衝突
        {
            //Destroy(gameObject); // しぼう
            rb2d.simulated = false; // 一瞬だけ物理演算切る(落下の加速度を0にするため)
            Invoke("positionn", 3); // 1秒遅れて(0, 0)に移動
            Invoke("simular", 4); // 2秒遅れて物理演算ふっかつ
            GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, 0); // RGB=1,透明度=0
            rb2d.velocity = Vector2.zero; // いちど加速度をゼロにする
            Instantiate(explosionPrefab, transform.position, Quaternion.identity); // ばくはつ
            audioSource.PlayOneShot(SE_sibou01, 0.6F);
        }
    }

    void positionn() // 原点移動(0, 2.8)
    {
        // transformを取得
        Transform myTransform = this.transform;
        // ワールド座標を基準に、座標を取得
        Vector2 worldPos = myTransform.position;
        worldPos.x = 0.0f;    // ワールド座標を基準にした、x座標を0に変更
        worldPos.y = 2.8f;    // ワールド座標を基準にした、y座標を0に変更
        // worldPos.z = 1.0f;    // ワールド座標を基準にした、z座標を1に変更 // 3Dの場合、べくたーも3に
        myTransform.position = worldPos;  // ワールド座標での座標を設定
        GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, 1); // RGB=1,透明度=1
        Instantiate(explosionPrefab2, transform.position, Quaternion.identity); // 復活
    }
    void simular() // 物理演算ON
    {
        rb2d.simulated = true; // Rigidbody2DのSimulatedをON
    }

    public void Migi_Button() // タッチパネルでの移動→
    {
        direction2 = 1f;
        GetComponent<SpriteRenderer>().sprite = Rup;// 右上がぞう
    }
    public void Hidari_Button() // タッチパネルでの移動←
    {
        direction2 = -1f;
        GetComponent<SpriteRenderer>().sprite = Lup;// ←上がぞう
    }
    public void ButtonExit() // ボタンはなし
    {
        direction2 = 0f;
    }
    public void Jump_Button() // ジャンプボタン
    {
        if (jump_point >= 1)
        {            
            rb2d.velocity = Vector2.zero; // いちど加速度をゼロにする
            rb2d.AddForce(Vector2.up * flap);
            jump_point -= 1;
        }
    }


}


おしまい。

 

0 件のコメント:

コメントを投稿