Warning: Declaration of action_plugin_linebreak::register(&$controller) should be compatible with DokuWiki_Action_Plugin::register(Doku_Event_Handler $controller) in /home/stepism/www/ue4/wiki/lib/plugins/linebreak/action.php on line 41

Warning: Declaration of action_plugin_markdownextra::register(&$controller) should be compatible with DokuWiki_Action_Plugin::register(Doku_Event_Handler $controller) in /home/stepism/www/ue4/wiki/lib/plugins/markdownextra/action.php on line 16

Warning: Declaration of action_plugin_syntaxhighlighter3_action::register(Doku_Event_Handler &$controller) should be compatible with DokuWiki_Action_Plugin::register(Doku_Event_Handler $controller) in /home/stepism/www/ue4/wiki/lib/plugins/syntaxhighlighter3/action/action.php on line 28
069 2段ジャンプを実装する(AddForceを使った場合) [stepism@UE4メモ]

ユーザ用ツール

サイト用ツール


wiki:unity:tips:069

以前のリビジョンの文書です —-


Warning: Declaration of syntax_plugin_linebreak::handle($match, $state, $pos, &$handler) should be compatible with DokuWiki_Syntax_Plugin::handle($match, $state, $pos, Doku_Handler $handler) in /home/stepism/www/ue4/wiki/lib/plugins/linebreak/syntax.php on line 52

Warning: Declaration of syntax_plugin_linebreak::render($mode, &$renderer, $data) should be compatible with DokuWiki_Syntax_Plugin::render($format, Doku_Renderer $renderer, $data) in /home/stepism/www/ue4/wiki/lib/plugins/linebreak/syntax.php on line 74

Warning: Declaration of syntax_plugin_markdownextra::handle($match, $state, $pos, &$handler) should be compatible with DokuWiki_Syntax_Plugin::handle($match, $state, $pos, Doku_Handler $handler) in /home/stepism/www/ue4/wiki/lib/plugins/markdownextra/syntax.php on line 38

Warning: Declaration of syntax_plugin_markdownextra::render($mode, &$renderer, $data) should be compatible with DokuWiki_Syntax_Plugin::render($format, Doku_Renderer $renderer, $data) in /home/stepism/www/ue4/wiki/lib/plugins/markdownextra/syntax.php on line 47

Warning: Declaration of syntax_plugin_syntaxhighlighter3_syntax::handle($match, $state, $pos, &$handler) should be compatible with DokuWiki_Syntax_Plugin::handle($match, $state, $pos, Doku_Handler $handler) in /home/stepism/www/ue4/wiki/lib/plugins/syntaxhighlighter3/syntax/syntax.php on line 53

Warning: Declaration of syntax_plugin_syntaxhighlighter3_syntax::render($mode, &$renderer, $data) should be compatible with DokuWiki_Syntax_Plugin::render($format, Doku_Renderer $renderer, $data) in /home/stepism/www/ue4/wiki/lib/plugins/syntaxhighlighter3/syntax/syntax.php on line 82

069 2段ジャンプを実装する(AddForceを使った場合)

概要

rigidbody.AddForceを使って2段ジャンプを実装します。
2段目のジャンプを1段目と同じ挙動にするためにはrigidbody.velocity を初期化します。

3Dの場合

using UnityEngine; 
using System.Collections; 
 
public class Player : MonoBehaviour 
{ 
	public const int MAX_JUMP_COUNT = 2;	// ジャンプできる回数。 
	public float force = 5.0f; 
 
	private int jumpCount = 0; 
	private bool isJump = false; 
 
	void Update() 
	{ 
		// クリックでジャンプ。 
		if( jumpCount < MAX_JUMP_COUNT && Input.GetMouseButtonDown(0) ) 
		{ 
			isJump = true; 
		} 
	} 
 
	void FixedUpdate() 
	{ 
		if( isJump ) 
		{ 
			// 速度をクリアして2回目のジャンプも1回目と同じ挙動にする。 
			rigidbody.velocity = Vector3.zero; 
 
			// ジャンプさせる。 
			rigidbody.AddForce( Vector3.up * force, ForceMode.Impulse ); 
 
			// ジャンプ回数をカウント。 
			jumpCount++; 
 
			// ジャンプを許可する。 
			isJump = false; 
		} 
	} 
 
	private void OnCollisionEnter( Collision collision ) 
	{ 
		// 地面との当たり。 
		if (collision.gameObject.name == "Floor") 
		{ 
			jumpCount = 0; 
		} 
	} 
} 

操作するPlayerのGameObjectには当たり判定用にBox Collider、物理属性付加のためRigidBodyコンポーネントを追加しています。
地面となるFloorのGameObjectにはBox Collider コンポーネントのみ追加しています。

2Dの場合

3Dとの違いは当たり判定や物理属性付加に、2D用のrigidbody2DとBox Collider2Dを使っている点です。
ジャンプに加える力forceも2D用に調整している点に注目して下さい。

using UnityEngine; 
using System.Collections; 
 
public class Player : MonoBehaviour 
{ 
	private const int MAX_JUMP_COUNT = 2;	// ジャンプできる回数。 
	private float force = 250.0f; 
 
	private int jumpCount = 0; 
	private bool isJump = false; 
 
	void Update() 
	{ 
		// クリックでジャンプ。 
		if( jumpCount < MAX_JUMP_COUNT && Input.GetMouseButtonDown(0) ) 
		{ 
			isJump = true; 
		} 
	} 
 
	void FixedUpdate() 
	{ 
		if( isJump ) 
		{ 
			// 速度をクリアした2回目のジャンプも1回目と同じ挙動にする。 
			rigidbody2D.velocity = Vector2.zero; 
 
			// ジャンプさせる。 
			rigidbody2D.AddForce( Vector2.up * force ); 
 
			// ジャンプ回数をカウント。 
			jumpCount++; 
 
			// ジャンプを許可する。 
			isJump = false; 
		} 
	} 
 
	private void OnCollisionEnter2D( Collision2D collision ) 
	{ 
		if (collision.gameObject.name == "Floor") 
		{ 
			jumpCount = 0; 
		} 
	} 
} 
wiki/unity/tips/069.1415869348.txt.gz · 最終更新: 2015/01/06 14:16 (外部編集)