0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

はしご備忘録

Posted at
c# Ladder.cs

	public void InitLadder(GameObject _LadderObject)
	{
		//重力off 梯子オブジェクトのx座標をプレイヤーのx座標にあわせる  トリガーにする(床貫通用)
		g_Rb2D.gravityScale = 0.0f;
		transform.position = new Vector2(_LadderObject.transform.position.x, transform.position.y);
		GetComponent<BoxCollider2D>().isTrigger = true;

                //最大y値=梯子オブジェクトの中心y座標+梯子のローカルスケール/2+プレイヤーのローカルスケール/2
		MaxLenLadder = _LadderObject.transform.position.y + _LadderObject.transform.localScale.y * 0.5f
                               + transform.localscale.y*0.5f;
		MinLenLadder = _LadderObject.transform.position.y - _LadderObject.transform.localScale.y * 0.5f
                                +transform.localscale.y*0.5f;
	}

    void UpdateLadder()
	{
        float MovementY=Input.GeyAxis("Horizontal");
        //上り
		if (MovementY> 0.0f)
		{
			if (MaxLenLadder > transform.position.y)
			{
				transform.position += new Vector3(0, 0.03f);
			}
		}
        //下り
		else if (MovementY < 0.0f)
		{
			if (MinLenLadder < transform.position.y)
			{
				transform.position -= new Vector3(0, 0.03f);
			}
		}
	}

	public void UninitLadder()
	{
		//重力on トリガーオフ
		g_Rb2D.gravityScale = 1.0f;
		GetComponent<BoxCollider2D>().isTrigger = false;
	}

最大最小のところがややこしい気がするので図で表示(緑の枠が当たり判定)

はしご.png

使い方
足がはしごの当たり判定にふれたとき、~キーが押されたらInitLadder()呼び出し
UpdateLadderをUpdateで呼び出し(ほかの操作をしないようにする)
足がはしごにふの当たり判定れたとき、~キーが押されたらUnInitLadder()呼び出しで上り下り終了

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?