ゲームプログラマーの寄り道

ゲームプログラマの寄り道

ソースコード公開系ブログ

ソースコード公開系ブログ

ゲームパッドを使用した4人同時入力

assetstore.unity.com
このアセットを使用した4人入力です。

過去のクソコードです。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GamepadInput; 
public class Player : Type
{
    //プレイヤーの動くスピード
    public float speed;
    //プレイヤーの体力
    public int HP;
    public bool shot;
    GameObject child;
	public GameObject clown;
    public GameObject Resurrection;
    PlayerGeneretion generetion;
    public int PlayerNumber;
    public Vector3 V3;
    TimerScript timerScript;
    public AudioClip DamageSound;
    public AudioClip ResuscitationSound;
    public AudioClip ItemSound;
    public AudioClip ItemSoundRare;
    public int sound;
    public int soundRare;
    public float ShotDelete;
    public bool alive;
	GameObject Obj;
    Phase ph;
	bool one=true;
    public enum PlayerNo
    {
        none,
        one,
        two,
        three,
        four
    }
    public PlayerNo playerNo;

    //初期化
    void Start()
    {
        alive = true;
        speed = 6;
        ShotDelete = 15;
        V3 = transform.position;
        HP = 3;
        child = transform.Find("shot_point").gameObject;
        timerScript= GameObject.Find("Canvas").GetComponent<TimerScript>();
        generetion = GameObject.Find("GeneretionPlayer").GetComponent<PlayerGeneretion>();
        ph = GameObject.Find("Phase").GetComponent<Phase>();
    }
    void Update()
    {
		if(Rank.win== (int)playerNo&&one)
		{
			Obj = (GameObject)Instantiate(clown, this.transform.position+new Vector3(0,3,0), Quaternion.identity);
			Obj.transform.parent = transform;
			one = false;
		}else if(Rank.win != (int)playerNo)
		{
			foreach (Transform child in transform)
			{
				if (child.name == "SI_CrownIcon(Clone)")
				{
					GameObject.Destroy(child.gameObject);
				}
			}
			one = true;
		}
        if (HP <= 0)
        {
            alive = false;
            ++sound;
            if (sound > 120)
            {
                sound = 0;
                Destroy();
                Destroy(gameObject);
            }
        }
        Movingrotation();
    }
    //移動回転処理
    private void Movingrotation()
    {
        GamepadState keyState1 = GamePad.GetState(GamePad.Index.One);
        GamepadState keyState2 = GamePad.GetState(GamePad.Index.Two);
        GamepadState keyState3 = GamePad.GetState(GamePad.Index.Three);
        GamepadState keyState4 = GamePad.GetState(GamePad.Index.Four);
        if ((alive == true&&timerScript.count>0)&&ph.Starttime == true)
        {
            //1P
            if (playerNo == PlayerNo.one)
            {
                //押されたキーの向きに向く
                if (keyState1.Down || keyState1.Up || Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.DownArrow))
                {
                    if (keyState1.Up || Input.GetKey(KeyCode.UpArrow))
                    {
                        transform.eulerAngles = new Vector3(0, 0, 0);
                    }
                    else
                    {
                        transform.eulerAngles = new Vector3(0, 180, 0);
                    }
                    //今向いている方向に移動する
                    transform.position += transform.forward * speed * Time.deltaTime;
                }else
                if (keyState1.Right || keyState1.Left || Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.RightArrow))
                {
                    if (keyState1.Right || Input.GetKey(KeyCode.RightArrow))
                    {
                        transform.eulerAngles = new Vector3(0, 90, 0);
                    }
                    else
                    {
                        transform.eulerAngles = new Vector3(0, -90, 0);
                    }
                    //今向いている方向に移動する
                    transform.position += transform.forward * speed * Time.deltaTime;
                }
            }
            //2P
            if (playerNo == PlayerNo.two)
            {
                //押されたキーの向きに向く
                if (keyState2.Down || keyState2.Up || Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S))
                {
                    if (keyState2.Up || Input.GetKey(KeyCode.W))
                    {
                        transform.eulerAngles = new Vector3(0, 0, 0);
                    }
                    else
                    {
                        transform.eulerAngles = new Vector3(0, 180, 0);
                    }
                    //今向いている方向に移動する
                    transform.position += transform.forward * speed * Time.deltaTime;
                }
				else
				if (keyState2.Right || keyState2.Left || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D))
                {
                    if (keyState2.Right || Input.GetKey(KeyCode.D))
                    {
                        transform.eulerAngles = new Vector3(0, 90, 0);
                    }
                    else
                    {
                        transform.eulerAngles = new Vector3(0, -90, 0);
                    }
                    //今向いている方向に移動する
                    transform.position += transform.forward * speed * Time.deltaTime;
                }
            }
            //3P
            if ((playerNo == PlayerNo.three))
            {
                //押されたキーの向きに向く
                if (keyState3.Down || keyState3.Up || Input.GetKey(KeyCode.T) || Input.GetKey(KeyCode.G))
                {
                    if (keyState3.Up || Input.GetKey(KeyCode.T))
                    {
                        transform.eulerAngles = new Vector3(0, 0, 0);
                    }
                    else
                    {
                        transform.eulerAngles = new Vector3(0, 180, 0);
                    }
                    //今向いている方向に移動する
                    transform.position += transform.forward * speed * Time.deltaTime;
                }
				else
				if (keyState3.Right || keyState3.Left || Input.GetKey(KeyCode.F) || Input.GetKey(KeyCode.H))
                {
                    if (keyState3.Right || Input.GetKey(KeyCode.H))
                    {
                        transform.eulerAngles = new Vector3(0, 90, 0);
                    }
                    else
                    {
                        transform.eulerAngles = new Vector3(0, -90, 0);
                    }
                    //今向いている方向に移動する
                    transform.position += transform.forward * speed * Time.deltaTime;
                }
            }
            //4P
            if (playerNo == PlayerNo.four)
            {
                //押されたキーの向きに向く
                if (keyState4.Down || keyState4.Up || Input.GetKey(KeyCode.I) || Input.GetKey(KeyCode.K))
                {
                    if (keyState4.Up || Input.GetKey(KeyCode.I))
                    {
                        transform.eulerAngles = new Vector3(0, 0, 0);
                    }
                    else
                    {
                        transform.eulerAngles = new Vector3(0, 180, 0);
                    }
                    //今向いている方向に移動する
                    transform.position += transform.forward * speed * Time.deltaTime;
                }
				else
				if (keyState4.Right || keyState4.Left || Input.GetKey(KeyCode.J) || Input.GetKey(KeyCode.L))
                {
                    if (keyState4.Right || Input.GetKey(KeyCode.L))
                    {
                        transform.eulerAngles = new Vector3(0, 90, 0);
                    }
                    else
                    {
                        transform.eulerAngles = new Vector3(0, -90, 0);
                    }
                    //今向いている方向に移動する
                    transform.position += transform.forward * speed * Time.deltaTime;
                }
            }
        }
    }
    private void OnTriggerEnter(Collider other)
    {
        //当たったものが弾なら入る
        if (other.gameObject.GetComponent<Type>().objtype == Objtype.bullet)
        {
            //弾の発射元じゃないならHPを減らす
            if (other.gameObject.GetComponent<Bullet>().ShotPlayer != gameObject)
            {
                --HP;
                if (HP != 0)
                {
                    GetComponent<AudioSource>().PlayOneShot(DamageSound);
                }
                else
                {
                    GetComponent<AudioSource>().PlayOneShot(ResuscitationSound);
                }
            }
        }
        if (other.GetComponent<Type>().objtype == Objtype.item)
        {
            soundRare = Random.Range(0, 10);
            if (soundRare != 5)
            {
                GetComponent<AudioSource>().PlayOneShot(ItemSound);
            }
            else
            {
                GetComponent<AudioSource>().PlayOneShot(ItemSoundRare);
            }
        }
    }
    private void Destroy()
    {
        generetion.vs[PlayerNumber] = false;
        //Instantiate(Resurrection, V3, Quaternion.identity);
        switch ((int)playerNo)
        {
            case 1:
                ++Rank.no_1;
                break;
            case 2:
                ++Rank.no_2;
                break;
            case 3:
                ++Rank.no_3;
                break;
            case 4:
                ++Rank.no_4;
                break;
            default:
                break;
        }
    }
}
***募集***
一緒にブログを運営してくれる人募集しています。
主な作業は修理作業です。画像足りなかったら拾ってくるとか、誤字の修正などです。
見出し増やすなど、見た目に関する所お願いします。
Twitterに連絡くれれば反応できます。

しんーーご (@shi_k_7) | Twitter