<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>チュートリアル アーカイブ - だくまたゲーム制作ブログ</title>
	<atom:link href="https://d9m.one/tag/tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>https://d9m.one/tag/tutorial/</link>
	<description></description>
	<lastBuildDate>Mon, 08 Jan 2024 05:33:57 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
	<item>
		<title>紹介～横スクロールシューティング</title>
		<link>https://d9m.one/sidestg/</link>
					<comments>https://d9m.one/sidestg/#respond</comments>
		
		<dc:creator><![CDATA[だくまた]]></dc:creator>
		<pubDate>Wed, 12 Oct 2022 15:07:46 +0000</pubDate>
				<category><![CDATA[Unity]]></category>
		<category><![CDATA[チュートリアル]]></category>
		<guid isPermaLink="false">https://d9m.one/?p=1639</guid>

					<description><![CDATA[<p>Unityを使った横スクロール型のシューティング完成までの操作手順を書いた記事の紹介です。 ここでは完成品を遊べます。 アセットストアで無料公開されている「Warped Space Shooter」をお借りして作成してい [&#8230;]</p>
<p>投稿 <a href="https://d9m.one/sidestg/">紹介～横スクロールシューティング</a> は <a href="https://d9m.one">だくまたゲーム制作ブログ</a> に最初に表示されました。</p>
]]></description>
										<content:encoded><![CDATA[
<p><a href="https://unity.com/ja">Unity</a>を使った横スクロール型のシューティング完成までの操作手順を書いた記事の紹介です。 <br>ここでは完成品を遊べます。</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6834e6beda7cd&quot;}" data-wp-interactive="core/image" class="wp-block-image aligncenter size-medium wp-lightbox-container"><img fetchpriority="high" decoding="async" width="300" height="212" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on-async--click="actions.showLightbox" data-wp-on-async--load="callbacks.setButtonStyles" data-wp-on-async-window--resize="callbacks.setButtonStyles" src="https://d9m.one/wp-content/uploads/2022/10/WarpedSpaceShooter-300x212.png" alt="Warped Space Shooter" class="wp-image-2038" srcset="https://d9m.one/wp-content/uploads/2022/10/WarpedSpaceShooter-300x212.png 300w, https://d9m.one/wp-content/uploads/2022/10/WarpedSpaceShooter-768x542.png 768w, https://d9m.one/wp-content/uploads/2022/10/WarpedSpaceShooter.png 819w" sizes="(max-width: 300px) 100vw, 300px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			aria-label="拡大する"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on-async--click="actions.showLightbox"
			data-wp-style--right="state.imageButtonRight"
			data-wp-style--top="state.imageButtonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption"><strong>Warped Space Shooter</strong></figcaption></figure>



<p>アセットストアで無料公開されている「Warped Space Shooter」をお借りして作成しています。<br>また、基本操作、スクリプト、UI、テキストメッシュプロ、サウンド、アニメーションといったあたりを練習できます。 </p>



<p>自機はマウスを追いかけるように移動し、弾は自動で発射されます。 <br>敵は５パターンのウェーブが繰り返す形で進行します。 <br>クリアはありません。</p>



<p> １本のゲームを作るまでの流れを体験してみてください。 </p>



<p>対象：初心者、Unityに初めて触る方<br>使用バージョン：2021.3</p>



<iframe src="/game/sidestg/index.html" width="780" height="465" scrolling="no" frameborder="0"></iframe>



<p>シューティング作成の関連記事です。</p>



<ul class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li><a href="https://d9m.one/sidestg/">横スクロールシューティング紹介</a></li>



<li><a href="https://d9m.one/sidestg1/">横スクロールシューティング①</a></li>



<li><a href="https://d9m.one/sidestg2/">横スクロールシューティング②</a></li>



<li><a href="https://d9m.one/sidestg3/">横スクロールシューティング③</a></li>



<li><a href="https://d9m.one/sidestg4/">横スクロールシューティング④</a></li>



<li><a href="https://d9m.one/sidestg5/">横スクロールシューティング⑤</a></li>



<li><a href="https://d9m.one/sidestg6/">横スクロールシューティング⑥</a></li>
</ul>
<p>投稿 <a href="https://d9m.one/sidestg/">紹介～横スクロールシューティング</a> は <a href="https://d9m.one">だくまたゲーム制作ブログ</a> に最初に表示されました。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://d9m.one/sidestg/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>スコア制御～横スクロールシューティング⑥</title>
		<link>https://d9m.one/sidestg6/</link>
					<comments>https://d9m.one/sidestg6/#respond</comments>
		
		<dc:creator><![CDATA[だくまた]]></dc:creator>
		<pubDate>Tue, 11 Oct 2022 13:41:34 +0000</pubDate>
				<category><![CDATA[Unity]]></category>
		<category><![CDATA[チュートリアル]]></category>
		<guid isPermaLink="false">https://d9m.one/?p=1618</guid>

					<description><![CDATA[<p>目次 ●はじめに●テキスト作成▷テキストUI作成▷テキストUI設定●スコア制御▷スコア制御の準備▷テキストとして表示●スコア加算▷スコア加算機能追加▷加算するスコアを保存▷破壊時にスコア加算●完成 ●はじめに 2D横スク [&#8230;]</p>
<p>投稿 <a href="https://d9m.one/sidestg6/">スコア制御～横スクロールシューティング⑥</a> は <a href="https://d9m.one">だくまたゲーム制作ブログ</a> に最初に表示されました。</p>
]]></description>
										<content:encoded><![CDATA[

  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-4" checked><label class="toc-title" for="toc-checkbox-4">目次</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">●はじめに</a></li><li><a href="#toc2" tabindex="0">●テキスト作成</a><ol><li><a href="#toc3" tabindex="0">▷テキストUI作成</a></li><li><a href="#toc4" tabindex="0">▷テキストUI設定</a></li></ol></li><li><a href="#toc5" tabindex="0">●スコア制御</a><ol><li><a href="#toc6" tabindex="0">▷スコア制御の準備</a></li><li><a href="#toc7" tabindex="0">▷テキストとして表示</a></li></ol></li><li><a href="#toc8" tabindex="0">●スコア加算</a><ol><li><a href="#toc9" tabindex="0">▷スコア加算機能追加</a></li><li><a href="#toc10" tabindex="0">▷加算するスコアを保存</a></li><li><a href="#toc11" tabindex="0">▷破壊時にスコア加算</a></li></ol></li><li><a href="#toc12" tabindex="0">●完成</a></li></ol>
    </div>
  </div>

<h2 class="wp-block-heading"><span id="toc1">●はじめに</span></h2>



<p>2D横スクロールシューティング作成の６回目、最終回になります。<br>アセットストアで無料公開されている「<a rel="noopener" href="https://assetstore.unity.com/packages/2d/characters/warped-space-shooter-181590#content" target="_blank">Warped Space Shooter</a>」をお借りして作成しています。</p>



<p>今回は「スコア制御」を追加し、一旦完成とします。</p>



<iframe src="/game/sidestg6/index.html" width="780" height="465" scrolling="no" frameborder="0"></iframe>



<p>　↑これが完成します。</p>



<h2 class="wp-block-heading"><span id="toc2">●テキスト作成</span></h2>



<h3 class="wp-block-heading"><span id="toc3">▷テキストUI作成</span></h3>



<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-image size-full"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/10/SideSTG_056.png" target="_blank"><img decoding="async" width="298" height="514" src="https://d9m.one/wp-content/uploads/2022/10/SideSTG_056.png" alt="テキストUIの作成とTextMeshProのパッケージ追加" class="wp-image-1627" srcset="https://d9m.one/wp-content/uploads/2022/10/SideSTG_056.png 298w, https://d9m.one/wp-content/uploads/2022/10/SideSTG_056-174x300.png 174w" sizes="(max-width: 298px) 100vw, 298px" /></a><figcaption class="wp-element-caption">テキストUIの作成とTextMeshProのパッケージ追加</figcaption></figure>
</div>



<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow"></div>
</div>



<p>・解説</p>



<p>スコアを表示するためのテキストUIを作成します。</p>



<p>・手順</p>



<p>「テキスト &#8211; TextMeshPro」を作成し、TextMeshProのパッケージをインポートします。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>ヒエラルキーの右上にある「＋」をクリック</li>



<li>「UI&gt;テキスト &#8211; TextMeshPro」をクリック</li>



<li>TMP Importerの「Import TMP Essentials」をクリック</li>



<li>TMP Importerの右上にある「✕」をクリック</li>
</ol>



<h3 class="wp-block-heading"><span id="toc4">▷テキストUI設定</span></h3>



<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-image size-full"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/10/SideSTG_057.png" target="_blank"><img decoding="async" width="632" height="491" src="https://d9m.one/wp-content/uploads/2022/10/SideSTG_057.png" alt="テキストUI設定" class="wp-image-1628" srcset="https://d9m.one/wp-content/uploads/2022/10/SideSTG_057.png 632w, https://d9m.one/wp-content/uploads/2022/10/SideSTG_057-300x233.png 300w" sizes="(max-width: 632px) 100vw, 632px" /></a><figcaption class="wp-element-caption">テキストUI設定</figcaption></figure>
</div>



<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow"></div>
</div>



<p>・解説</p>



<p>画面の右上に寄せてテキストを表示します。</p>



<p>・手順</p>



<p>キャンバスをスクリーン範囲で表示するようにします。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>ヒエラルキーの「Canvas」をクリック</li>



<li>インスペクターの「Canvas&gt;レンダーモード」を「スクリーンスペース &#8211; カメラ」に変更</li>



<li>ヒエラルキーの「Main Camera」をインスペクターの「レンダーカメラ」にドラッグ＆ドロップ</li>
</ol>



<p>テキストUIのアンカーを右上に指定します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>ヒエラルキーの「Canvas&gt;Text (TMP)」をクリック</li>



<li>インスペクターの「Rect Transform」にある左上アイコンをクリック</li>



<li>SHIFTとALTを押しながらアンカープリセットの「top &#8211; right」をクリック</li>
</ol>



<p>テキスト整列を右上に配置し、TextMeshProを設定します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>インスペクターの「Rect Transform&gt;位置X」に(-10)を入力</li>



<li>インスペクターの「Rect Transform&gt;幅」に(200)を入力</li>



<li>インスペクターの「Rect Transform&gt;高さ」に(50)を入力</li>



<li>インスペクターの「TextMeshPro &#8211; Text&gt;Text Input」のテキスト入力に「1234567890」を入力</li>



<li>インスペクターの「TextMeshPro &#8211; Text&gt;Main Settings&gt;Font Style&gt;B」をクリック</li>



<li>インスペクターの「TextMeshPro &#8211; Text&gt;Main Settings&gt;Font Size」に(16)を入力</li>



<li>インスペクターの「TextMeshPro &#8211; Text&gt;Main Settings&gt;Alignment&gt;Right」をクリック</li>



<li>インスペクターの「TextMeshPro &#8211; Text&gt;Main Settings&gt;Alignment&gt;Top」をクリック</li>
</ol>



<h2 class="wp-block-heading"><span id="toc5">●スコア制御</span></h2>



<h3 class="wp-block-heading"><span id="toc6">▷スコア制御の準備</span></h3>



<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-image size-full"><img decoding="async" width="281" height="359" src="https://d9m.one/wp-content/uploads/2022/10/SideSTG_058.png" alt="自機のインスペクター情報" class="wp-image-1630" srcset="https://d9m.one/wp-content/uploads/2022/10/SideSTG_058.png 281w, https://d9m.one/wp-content/uploads/2022/10/SideSTG_058-235x300.png 235w" sizes="(max-width: 281px) 100vw, 281px" /><figcaption class="wp-element-caption">自機のインスペクター情報</figcaption></figure>
</div>



<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow"></div>
</div>



<p>・解説</p>



<p>スコアそのものと、表示先とするテキストを保持するための変数を作成します。</p>



<p>・手順</p>



<p>テキストUIを使用するためTMProのusingを追加します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>プロジェクトの「Assets&gt;Player」をダブルクリック</li>



<li>using最後の行に以下のusingを追加</li>
</ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Player.cs" data-lang="C#"><code>using TMPro;
</code></pre></div>



<p>スコア値、テキストUIを保持する変数を追加します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>「ゲーム管理オブジェクト」の下に以下の変数定義を追加</li>
</ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Player.cs" data-lang="C#"><code>    public TextMeshProUGUI scoreText;   // スコアテキスト
    int score;                          // スコア
</code></pre></div>



<p>スクリプトを保存してビルドします。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>CTRLを押しながらSキーを押してスクリプト保存</li>



<li>Unityエディタをクリックしてビルド</li>
</ol>



<h3 class="wp-block-heading"><span id="toc7">▷テキストとして表示</span></h3>



<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-image size-full"><img decoding="async" width="281" height="359" src="https://d9m.one/wp-content/uploads/2022/10/SideSTG_059.png" alt="自機のインスペクター情報" class="wp-image-1631" srcset="https://d9m.one/wp-content/uploads/2022/10/SideSTG_059.png 281w, https://d9m.one/wp-content/uploads/2022/10/SideSTG_059-235x300.png 235w" sizes="(max-width: 281px) 100vw, 281px" /><figcaption class="wp-element-caption">自機のインスペクター情報</figcaption></figure>
</div>



<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow"></div>
</div>



<p>・解説</p>



<p>スコアとして保持している値を画面に表示します。</p>



<p>・手順</p>



<p>スコア表示処理を追加します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>プロジェクトの「Assets&gt;Player」をダブルクリック</li>



<li>Update関数内に以下の処理を追加</li>
</ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Player.cs" data-lang="C#"><code>        // スコア表示
        scoreText.text = score.ToString();
</code></pre></div>



<p>スクリプトを保存してビルドします。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>CTRLを押しながらSキーを押してスクリプト保存</li>



<li>Unityエディタをクリックしてビルド</li>
</ol>



<p>テキストUIをPlayerスクリプトに割り当てます。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>ヒエラルキーの「Player1」をクリック</li>



<li>インスペクターの「Player (スクリプト）&gt;Score Text」にあるボックスにヒエラルキーの「Canvas&gt;Text (TMP)」をドラッグ＆ドロップ</li>
</ol>



<h2 class="wp-block-heading"><span id="toc8">●スコア加算</span></h2>



<h3 class="wp-block-heading"><span id="toc9">▷スコア加算機能追加</span></h3>



<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-image size-full"><img decoding="async" width="820" height="425" src="https://d9m.one/wp-content/uploads/2022/10/SideSTG_060.png" alt="Playerスクリプトの最後にスコア加算機能を追加" class="wp-image-1632" srcset="https://d9m.one/wp-content/uploads/2022/10/SideSTG_060.png 820w, https://d9m.one/wp-content/uploads/2022/10/SideSTG_060-300x155.png 300w, https://d9m.one/wp-content/uploads/2022/10/SideSTG_060-768x398.png 768w" sizes="(max-width: 820px) 100vw, 820px" /><figcaption class="wp-element-caption">Playerスクリプトの最後にスコア加算機能を追加</figcaption></figure>
</div>



<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow"></div>
</div>



<p>・解説</p>



<p>スコアを加算できるようにします。</p>



<p>・手順</p>



<p>スコアを加算する処理を追加し、他のスクリプトから使えるようにします。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>プロジェクトの「Assets&gt;Player」をダブルクリック</li>



<li>Update関数の次に以下の関数を追加</li>
</ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Player.cs" data-lang="C#"><code>    // スコア加算
    public void AddScore(int value) 
    {
        score += value;
    }
</code></pre></div>



<p>スクリプトを保存してビルドします。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>CTRLを押しながらSキーを押してスクリプト保存</li>



<li>Unityエディタをクリックしてビルド</li>
</ol>



<h3 class="wp-block-heading"><span id="toc10">▷加算するスコアを保存</span></h3>



<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-image size-full"><img decoding="async" width="820" height="425" src="https://d9m.one/wp-content/uploads/2022/10/SideSTG_061.png" alt="Enemyスクリプトにスコア変数を追加" class="wp-image-1633" srcset="https://d9m.one/wp-content/uploads/2022/10/SideSTG_061.png 820w, https://d9m.one/wp-content/uploads/2022/10/SideSTG_061-300x155.png 300w, https://d9m.one/wp-content/uploads/2022/10/SideSTG_061-768x398.png 768w" sizes="(max-width: 820px) 100vw, 820px" /><figcaption class="wp-element-caption">Enemyスクリプトにスコア変数を追加</figcaption></figure>
</div>



<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow"></div>
</div>



<p>・解説</p>



<p>加算するスコアをEnemyスクリプトに持たせます。</p>



<p>・手順</p>



<p>Enemyスクリプトにint型の公開変数を追加します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>プロジェクトの「Assets&gt;Enemy」をダブルクリック</li>



<li>Start関数のに以下の関数を追加</li>
</ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Enemy.cs" data-lang="C#"><code>    public int score = 100;         // スコア
</code></pre></div>



<h3 class="wp-block-heading"><span id="toc11">▷破壊時にスコア加算</span></h3>



<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-image size-full"><img decoding="async" width="418" height="285" src="https://d9m.one/wp-content/uploads/2022/10/SideSTG_062.png" alt="敵を倒すとスコアが加算される" class="wp-image-1634" srcset="https://d9m.one/wp-content/uploads/2022/10/SideSTG_062.png 418w, https://d9m.one/wp-content/uploads/2022/10/SideSTG_062-300x205.png 300w, https://d9m.one/wp-content/uploads/2022/10/SideSTG_062-130x90.png 130w" sizes="(max-width: 418px) 100vw, 418px" /><figcaption class="wp-element-caption">敵を倒すとスコアが加算される</figcaption></figure>
</div>



<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow"></div>
</div>



<p>・解説</p>



<p>破壊されたら持っているスコアを自機が持つスコアに加算します。</p>



<p>・手順</p>



<p>破壊された時にPlayerスクリプトのスコア加算処理を呼び出します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>FixedUpdate関数の次に以下の処理を追加</li>
</ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Enemy.cs" data-lang="C#"><code>    // オブジェクト削除時に処理
    void OnDestroy()
    {
        // 自機取得
        GameObject obj = GameObject.FindGameObjectWithTag(&quot;Player&quot;);
        Player player = obj.GetComponent&lt;Player&gt;();
        // 自機が取得できた場合
        if (player != null)
        {
            // スコア加算
            player.AddScore(score);
        }
    }
</code></pre></div>



<p>スクリプトを保存してビルドします。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>CTRLを押しながらSキーを押してスクリプト保存</li>



<li>Unityエディタをクリックしてビルド</li>
</ol>



<p>プロジェクトを保存して動作を確認します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>CTRLを押しながらSキーを押してプロジェクト保存</li>



<li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー開始</li>



<li>敵を倒すとスコアが増えていくことを確認</li>



<li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー終了</li>
</ol>



<h2 class="wp-block-heading"><span id="toc12">●完成</span></h2>



<p>スコア制御を追加し、最低限シューティングと呼べそうな形になりました。</p>



<p>次は横スクロールアクション作ろうかな。</p>
<p>投稿 <a href="https://d9m.one/sidestg6/">スコア制御～横スクロールシューティング⑥</a> は <a href="https://d9m.one">だくまたゲーム制作ブログ</a> に最初に表示されました。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://d9m.one/sidestg6/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>敵ウェーブ～横スクロールシューティング⑤</title>
		<link>https://d9m.one/sidestg5/</link>
					<comments>https://d9m.one/sidestg5/#respond</comments>
		
		<dc:creator><![CDATA[だくまた]]></dc:creator>
		<pubDate>Wed, 21 Sep 2022 14:39:57 +0000</pubDate>
				<category><![CDATA[Unity]]></category>
		<category><![CDATA[チュートリアル]]></category>
		<guid isPermaLink="false">https://d9m.one/?p=1526</guid>

					<description><![CDATA[<p>目次 ●はじめに●敵ウェーブ作成▷ウェーブ作成準備▷前進する敵５機▷浮遊する隕石３つ▷前進する敵５機②▷カーブ前進する敵と大きな敵●ウェーブ制御▷キャラクターが動ける範囲の作成▷動ける範囲を出たら削除▷敵ウェーブ配置●完 [&#8230;]</p>
<p>投稿 <a href="https://d9m.one/sidestg5/">敵ウェーブ～横スクロールシューティング⑤</a> は <a href="https://d9m.one">だくまたゲーム制作ブログ</a> に最初に表示されました。</p>
]]></description>
										<content:encoded><![CDATA[

  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-6" checked><label class="toc-title" for="toc-checkbox-6">目次</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">●はじめに</a></li><li><a href="#toc2" tabindex="0">●敵ウェーブ作成</a><ol><li><a href="#toc3" tabindex="0">▷ウェーブ作成準備</a></li><li><a href="#toc4" tabindex="0">▷前進する敵５機</a></li><li><a href="#toc5" tabindex="0">▷浮遊する隕石３つ</a></li><li><a href="#toc6" tabindex="0">▷前進する敵５機②</a></li><li><a href="#toc7" tabindex="0">▷カーブ前進する敵と大きな敵</a></li></ol></li><li><a href="#toc8" tabindex="0">●ウェーブ制御</a><ol><li><a href="#toc9" tabindex="0">▷キャラクターが動ける範囲の作成</a></li><li><a href="#toc10" tabindex="0">▷動ける範囲を出たら削除</a></li><li><a href="#toc11" tabindex="0">▷敵ウェーブ配置</a></li></ol></li><li><a href="#toc12" tabindex="0">●完成</a></li></ol>
    </div>
  </div>

<h2 class="wp-block-heading"><span id="toc1">●はじめに</span></h2>



<p>2D横スクロールシューティング作成の５回目になります。<br>アセットストアで無料公開されている「<a rel="noopener" href="https://assetstore.unity.com/packages/2d/characters/warped-space-shooter-181590#content" target="_blank">Warped Space Shooter</a>」をお借りして作成しています。</p>



<p>今回は「敵ウェーブ」を作成し、遊べる形にします。</p>



<iframe loading="lazy" src="/game/sidestg5/index.html" width="780" height="465" scrolling="no" frameborder="0"></iframe>



<p>　↑これが完成します。</p>



<h2 class="wp-block-heading"><span id="toc2">●敵ウェーブ作成</span></h2>



<h3 class="wp-block-heading"><span id="toc3">▷ウェーブ作成準備</span></h3>



<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/09/SideSTG_043.png" target="_blank"><img decoding="async" width="353" height="566" src="https://d9m.one/wp-content/uploads/2022/09/SideSTG_043.png" alt="敵ウェーブ作成の準備完了" class="wp-image-1549" style="aspect-ratio:265/425" srcset="https://d9m.one/wp-content/uploads/2022/09/SideSTG_043.png 353w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_043-187x300.png 187w" sizes="(max-width: 353px) 100vw, 353px" /></a><figcaption class="wp-element-caption">敵ウェーブ作成の準備完了</figcaption></figure>
</div>



<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow"></div>
</div>



<p>・解説</p>



<p>ウェーブを作成するための準備を行います。</p>



<p>・手順</p>



<p>敵をプレハブ化し、隕石をヒエラルキーから削除します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>ヒエラルキーの「enemy1」をプロジェクトの「Assets」にドラッグ＆ドロップ</li>



<li>ヒエラルキーの「enemy1」をクリック</li>



<li>Deleteキーを押す</li>



<li>ヒエラルキーの「asteroid-small」をクリック</li>



<li>Deleteキーを押す</li>



<li>ヒエラルキーの「asteroid」をクリック</li>



<li>Deleteキーを押す</li>
</ol>



<h3 class="wp-block-heading"><span id="toc4">▷前進する敵５機</span></h3>



<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-image size-full"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/09/SideSTG_044.png" target="_blank"><img decoding="async" width="353" height="566" src="https://d9m.one/wp-content/uploads/2022/09/SideSTG_044.png" alt="敵ウェーブA作成" class="wp-image-1551" srcset="https://d9m.one/wp-content/uploads/2022/09/SideSTG_044.png 353w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_044-187x300.png 187w" sizes="(max-width: 353px) 100vw, 353px" /></a><figcaption class="wp-element-caption">敵ウェーブA作成</figcaption></figure>
</div>



<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/09/SideSTG_045.png" target="_blank"><img decoding="async" width="418" height="285" src="https://d9m.one/wp-content/uploads/2022/09/SideSTG_045.png" alt="前進する敵５機" class="wp-image-1552" style="aspect-ratio:314/214" srcset="https://d9m.one/wp-content/uploads/2022/09/SideSTG_045.png 418w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_045-300x205.png 300w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_045-130x90.png 130w" sizes="(max-width: 418px) 100vw, 418px" /></a><figcaption class="wp-element-caption">前進する敵５機</figcaption></figure>
</div>
</div>



<p>・解説</p>



<p>前進のみ行う敵を横に５機、上側に並べたウェーブ「waveA」を作成します。</p>



<p>・手順</p>



<p>空オブジェクトの子として敵プレハブを５機、上の方に配置します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>ヒエラルキーの左上にある「＋」をクリックし、「空のオブジェクトを作成」をクリック</li>



<li>「waveA」と入力してENTERを押す</li>



<li>インスペクターの「Transform&gt;位置」に(0, 0, 0)を入力</li>



<li>プロジェクトの「Assets&gt;enemy1」をヒエラルキーの「waveA」にドラッグ＆ドロップ</li>



<li>CTRLを押しながらDキーを押す</li>



<li>CTRLを押しながらDキーを押す</li>



<li>CTRLを押しながらDキーを押す</li>



<li>CTRLを押しながらDキーを押す</li>



<li>SHIFTを押しながらヒエラルキーの「waveA&gt;enemy1」をクリック</li>



<li>インスペクターの「Transform&gt;位置&gt;Y」に(3)を入力</li>



<li>インスペクターの「Transform&gt;位置&gt;X」に(#*-3+24)を入力</li>



<li>インスペクターの「Enemy(スクリプト)&gt;Cycle Count」に(0)を入力</li>



<li>インスペクターの「Enemy(スクリプト)&gt;Curve Length」に(0)を入力</li>
</ol>



<p>weveAをプレハブ化し、ヒエラルキーから削除します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>ヒエラルキーの「waveA」をプロジェクトの「Assets」にドラッグ＆ドロップ</li>



<li>ヒエラルキーの「waveA」をクリック</li>



<li>DELETEキーを押す</li>
</ol>



<h3 class="wp-block-heading"><span id="toc5">▷浮遊する隕石３つ</span></h3>



<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/09/SideSTG_046.png" target="_blank"><img decoding="async" width="353" height="566" src="https://d9m.one/wp-content/uploads/2022/09/SideSTG_046.png" alt="敵ウェーブB作成" class="wp-image-1553" style="aspect-ratio:265/425" srcset="https://d9m.one/wp-content/uploads/2022/09/SideSTG_046.png 353w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_046-187x300.png 187w" sizes="(max-width: 353px) 100vw, 353px" /></a><figcaption class="wp-element-caption">敵ウェーブB作成</figcaption></figure>
</div>



<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/09/SideSTG_047.png" target="_blank"><img decoding="async" width="418" height="285" src="https://d9m.one/wp-content/uploads/2022/09/SideSTG_047.png" alt="浮遊する隕石３つ" class="wp-image-1554" style="aspect-ratio:314/214" srcset="https://d9m.one/wp-content/uploads/2022/09/SideSTG_047.png 418w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_047-300x205.png 300w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_047-130x90.png 130w" sizes="(max-width: 418px) 100vw, 418px" /></a><figcaption class="wp-element-caption">浮遊する隕石３つ</figcaption></figure>
</div>
</div>



<p>・解説</p>



<p>上端、下端から画面内に向かって進む隕石を３つ配置します。</p>



<p>・手順</p>



<p>空オブジェクトを作成し、敵プレハブを配置してインスペクターを設定します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>ヒエラルキーの左上にある「＋」をクリックし、「空のオブジェクトを作成」をクリック</li>



<li>「waveB」と入力してENTERを押す</li>



<li>インスペクターの「Transform&gt;位置」に(0, 0, 0)を入力</li>



<li>プロジェクトの「Assets&gt;asteroid」をヒエラルキーの「waveB」にドラッグ＆ドロップ</li>



<li>インスペクターの「Transform&gt;位置&gt;X」に(-1)を入力</li>



<li>インスペクターの「Transform&gt;位置&gt;Y」に(5)を入力</li>



<li>インスペクターの「Enemy(スクリプト)&gt;Random Min Dir」に(45)を入力</li>



<li>インスペクターの「Enemy(スクリプト)&gt;Random Max Dir」に(90)を入力</li>
</ol>



<p>waveB内のasteroidを複製・調整します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>ヒエラルキーの「waveB&gt;asteroid」をクリック</li>



<li>CTRLを押しながらDキーを押す</li>



<li>インスペクターの「Transform&gt;位置&gt;X」に(3)を入力</li>



<li>インスペクターの「Transform&gt;位置&gt;Y」に(-5.5)を入力</li>



<li>インスペクターの「Enemy(スクリプト)&gt;Random Min Dir」に(270)を入力</li>



<li>インスペクターの「Enemy(スクリプト)&gt;Random Max Dir」に(315)を入力</li>



<li>ヒエラルキーの「waveB&gt;asteroid (1)」をクリック</li>



<li>CTRLを押しながらDキーを押す</li>



<li>インスペクターの「Transform&gt;位置&gt;X」に(7)を入力</li>



<li>インスペクターの「Transform&gt;位置&gt;Y」に(6)を入力</li>



<li>インスペクターの「Enemy(スクリプト)&gt;Random Min Dir」に(45)を入力</li>



<li>インスペクターの「Enemy(スクリプト)&gt;Random Max Dir」に(90)を入力</li>
</ol>



<p>weveBをプレハブ化し、ヒエラルキーから削除します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>ヒエラルキーの「waveB」をプロジェクトの「Assets」にドラッグ＆ドロップ</li>



<li>ヒエラルキーの「waveB」をクリック</li>



<li>DELETEキーを押す</li>
</ol>



<h3 class="wp-block-heading"><span id="toc6">▷前進する敵５機②</span></h3>



<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/09/SideSTG_048.png" target="_blank"><img decoding="async" width="353" height="566" src="https://d9m.one/wp-content/uploads/2022/09/SideSTG_048.png" alt="敵ウェーブC作成" class="wp-image-1555" style="aspect-ratio:265/425" srcset="https://d9m.one/wp-content/uploads/2022/09/SideSTG_048.png 353w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_048-187x300.png 187w" sizes="(max-width: 353px) 100vw, 353px" /></a><figcaption class="wp-element-caption">敵ウェーブC作成</figcaption></figure>
</div>



<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/09/SideSTG_049.png" target="_blank"><img decoding="async" width="418" height="285" src="https://d9m.one/wp-content/uploads/2022/09/SideSTG_049.png" alt="前進する敵５機②" class="wp-image-1556" style="aspect-ratio:314/214" srcset="https://d9m.one/wp-content/uploads/2022/09/SideSTG_049.png 418w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_049-300x205.png 300w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_049-130x90.png 130w" sizes="(max-width: 418px) 100vw, 418px" /></a><figcaption class="wp-element-caption">前進する敵５機②</figcaption></figure>
</div>
</div>



<p>・解説</p>



<p>前進のみ行う敵を横に５機、下側に並べたウェーブ「waveC」を作成します。</p>



<p>・手順</p>



<p>waveAを複製し、敵５機を下側へ移動させます。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>プロジェクトの「Assets&gt;waveA」をクリック</li>



<li>CTRLを押しながらDキーを押す</li>



<li>F2キーを押し、「waveC」と入力してENTERを押す</li>



<li>プロジェクトの「Assets&gt;waveC」をダブルクリック</li>



<li>ヒエラルキーの「waveC&gt;enemy1」をクリック</li>



<li>SHIFTキーを押しながらヒエラルキーの「waveC&gt;enemy1 (4)」をクリック</li>



<li>インスペクターの「Transform&gt;位置&gt;Y」に(-3)を入力</li>



<li>ヒエラルキー左上の「&lt;」をクリック</li>
</ol>



<h3 class="wp-block-heading"><span id="toc7">▷カーブ前進する敵と大きな敵</span></h3>



<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/09/SideSTG_050.png" target="_blank"><img decoding="async" width="353" height="566" src="https://d9m.one/wp-content/uploads/2022/09/SideSTG_050.png" alt="敵ウェーブD作成" class="wp-image-1557" style="aspect-ratio:265/425" srcset="https://d9m.one/wp-content/uploads/2022/09/SideSTG_050.png 353w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_050-187x300.png 187w" sizes="(max-width: 353px) 100vw, 353px" /></a><figcaption class="wp-element-caption">敵ウェーブD作成</figcaption></figure>
</div>



<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/09/SideSTG_051.png" target="_blank"><img decoding="async" width="418" height="285" src="https://d9m.one/wp-content/uploads/2022/09/SideSTG_051.png" alt="カーブ前進する敵と大きな敵" class="wp-image-1558" style="aspect-ratio:314/214" srcset="https://d9m.one/wp-content/uploads/2022/09/SideSTG_051.png 418w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_051-300x205.png 300w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_051-130x90.png 130w" sizes="(max-width: 418px) 100vw, 418px" /></a><figcaption class="wp-element-caption">カーブ前進する敵と大きな敵</figcaption></figure>
</div>
</div>



<p>・解説</p>



<p>カーブする敵６機と、前進する大きな敵を配置したウェーブ「waveD」を作成します。</p>



<p>・手順</p>



<p>空オブジェクトを作成して敵プレハブを配置、大きな敵として設定します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>ヒエラルキーの左上にある「＋」をクリックし、「空のオブジェクトを作成」をクリック</li>



<li>「waveD」と入力してENTERを押す</li>



<li>インスペクターの「Transform&gt;位置」に(0, 0, 0)を入力</li>



<li>プロジェクトの「Assets&gt;enemy1」をヒエラルキーの「waveD」にドラッグ＆ドロップ</li>



<li>インスペクターの「Transform&gt;位置」に(24, 0, 0)を入力</li>



<li>インスペクターの「Transform&gt;スケール」に(12, 12, 1)を入力</li>



<li>インスペクターの「Enemy (スクリプト)&gt;Cycle Count」に(0)を入力</li>



<li>インスペクターの「Enemy (スクリプト)&gt;Curve Length」に(0)を入力</li>



<li>インスペクターの「Hit (スクリプト)&gt;Energy」に(30)を入力</li>
</ol>



<p>カーブする敵を配置します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>プロジェクトの「Assets&gt;enemy1」をヒエラルキーの「waveD」にドラッグ＆ドロップ</li>



<li>インスペクターの「Transform&gt;位置」に(14, 0.5, 0)を入力</li>



<li>プロジェクトの「Assets&gt;enemy1」をヒエラルキーの「waveD」にドラッグ＆ドロップ</li>



<li>インスペクターの「Transform&gt;位置」に(15, -0.5, 0)を入力</li>



<li>インスペクターの「Enemy (スクリプト)&gt;Curve Length」に(-2)を入力</li>



<li>プロジェクトの「Assets&gt;enemy1」をヒエラルキーの「waveD」にドラッグ＆ドロップ</li>



<li>インスペクターの「Transform&gt;位置」に(17, -1.5, 0)を入力</li>



<li>インスペクターの「Enemy (スクリプト)&gt;Curve Length」に(-2.5)を入力</li>



<li>プロジェクトの「Assets&gt;enemy1」をヒエラルキーの「waveD」にドラッグ＆ドロップ</li>



<li>インスペクターの「Transform&gt;位置」に(18, 1.5, 0)を入力</li>



<li>インスペクターの「Enemy (スクリプト)&gt;Curve Length」に(2.5)を入力</li>



<li>プロジェクトの「Assets&gt;enemy1」をヒエラルキーの「waveD」にドラッグ＆ドロップ</li>



<li>インスペクターの「Transform&gt;位置」に(21, 2, 0)を入力</li>



<li>インスペクターの「Enemy (スクリプト)&gt;Curve Length」に(3)を入力</li>



<li>プロジェクトの「Assets&gt;enemy1」をヒエラルキーの「waveD」にドラッグ＆ドロップ</li>



<li>インスペクターの「Transform&gt;位置」に(22, -2, 0)を入力</li>



<li>インスペクターの「Enemy (スクリプト)&gt;Curve Length」に(-3)を入力</li>
</ol>



<p>weveDをプレハブ化し、ヒエラルキーから削除します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>ヒエラルキーの「waveD」をプロジェクトの「Assets」にドラッグ＆ドロップ</li>



<li>ヒエラルキーの「waveD」をクリック</li>



<li>DELETEキーを押す</li>
</ol>



<h2 class="wp-block-heading"><span id="toc8">●ウェーブ制御</span></h2>



<h3 class="wp-block-heading"><span id="toc9">▷キャラクターが動ける範囲の作成</span></h3>



<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-image size-full is-resized"><img decoding="async" width="346" height="574" src="https://d9m.one/wp-content/uploads/2022/09/SideSTG_052.png" alt="敵ウェーブ管理のインスペクター情報" class="wp-image-1562" style="aspect-ratio:260/424" srcset="https://d9m.one/wp-content/uploads/2022/09/SideSTG_052.png 346w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_052-181x300.png 181w" sizes="(max-width: 346px) 100vw, 346px" /><figcaption class="wp-element-caption">敵ウェーブ管理のインスペクター情報</figcaption></figure>
</div>



<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/09/SideSTG_053.png" target="_blank"><img decoding="async" width="418" height="285" src="https://d9m.one/wp-content/uploads/2022/09/SideSTG_053.png" alt="キャラクターが動ける範囲" class="wp-image-1560" style="aspect-ratio:314/214" srcset="https://d9m.one/wp-content/uploads/2022/09/SideSTG_053.png 418w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_053-300x205.png 300w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_053-130x90.png 130w" sizes="(max-width: 418px) 100vw, 418px" /></a><figcaption class="wp-element-caption">キャラクターが動ける範囲</figcaption></figure>
</div>
</div>



<p>・解説</p>



<p>ウェーブ管理スクリプト用に空のオブジェクトを作成し、<br>BoxColliderを使って画面外に出たキャラクターを判断できるようにします。</p>



<p>最初は動ける範囲内に既にいるため、スクリプト「Hit」で衝突してしまいます。<br>スクリプト「Hit」での竈突はタグを使って回避します。</p>



<p>・手順</p>



<p>空ゲームオブジェクトして位置を調整、</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>ヒエラルキー左上にある「＋」をクリックし、「空のオブジェクトを作成」をクリックします。</li>



<li>「WaveController」と入力し、ENTERを押す</li>



<li>インスペクターの「Transform&gt;位置」に(0, 0, 0)を入力</li>
</ol>



<p>BoxCollider2Dを割り当て、</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>インスペクター下部にある「コンポーネントを追加」をクリック</li>



<li>「Physics 2D&gt;Box Collider 2D」をクリック</li>
</ol>



<p>BoxCollider2Dを設定します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>インスペクターの「Box Collider 2D&gt;トリガーにする」のチェックを入れる</li>



<li>インスペクターの「Box Collider 2D&gt;サイズ」に(18, 10)を入力</li>
</ol>



<p>タグ「WaveController」を作成、設定します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>インスペクター上部のタグドロップダウンをクリック</li>



<li>「タグを追加…」をクリック</li>



<li>インスペクターのタグ欄右下にある「＋」をクリック</li>



<li>「WaveController」と入力してENTERを押す</li>



<li>ヒエラルキーの「WaveController」をクリック</li>



<li>インスペクター上部のタグドロップダウンをクリック</li>



<li>「WaveController」をクリック</li>
</ol>



<p>スクリプト「Hit」の衝突処理でウェーブ管理と衝突しないようにします。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>プロジェクトの「Assets&gt;Hit」をダブルクリック</li>



<li>OnTriggerEnter2D関数内の最初に以下の処理を追加</li>
</ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Hit.cs" data-lang="C#"><code>        // ウェーブ管理とは衝突させない
        if (collider.gameObject.tag == &quot;WaveController&quot;)
        {
            return;
        }
</code></pre></div>



<h3 class="wp-block-heading"><span id="toc10">▷動ける範囲を出たら削除</span></h3>



<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/09/SideSTG_054.png" alt="弾が緑枠外に出ると消える" class="wp-image-1561" style="aspect-ratio:314/214"/><figcaption class="wp-element-caption">弾が緑枠外に出ると消える</figcaption></figure>
</div>



<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow"></div>
</div>



<p>・解説</p>



<p>画面外に出たら敵や弾を削除するスクリプトを作成します。</p>



<p>・手順</p>



<p>敵ウェーブを管理する変数を作成します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>プロジェクトの「Assets」を右クリックし、「作成＞C# スクリプト」をクリック</li>



<li>「WaveController」と入力してENTERを押す</li>



<li>プロジェクトの「Assets&gt;WaveController」をヒエラルキーの「WaveController」にドラッグ＆ドロップ</li>



<li>プロジェクトの「Assets&gt;WaveController」をダブルクリック</li>



<li>Start関数の前に以下の変数を追加</li>
</ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="WaveController.cs" data-lang="C#"><code>    public List&lt;GameObject&gt; waves;              // ウェーブ一覧

    int nextWaveIndex = 0;                      // 次のウェーブ位置
    GameObject currentWave;                     // 現在のウェーブ
    Vector3 hidePos;                            // カメラの後ろ位置
</code></pre></div>



<p>弾をカメラの後ろ位置へ移動するため、Startでカメラ後ろ位置を計算しておきます。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>Start関数内に以下の処理を追加</li>
</ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="WaveController.cs" data-lang="C#"><code>        // カメラの後ろ位置
        hidePos = Camera.main.transform.position - Camera.main.transform.forward;
</code></pre></div>



<p>敵が居なくなったら次のウェーブを作成する処理を作成します。<br>最後のウェーブだった場合、敵ウェーブ総数で割った余りを次に作成するウェーブ番号とします。<br>結果、敵ウェーブを永久に繰り返すようになります。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>Update関数内に以下の処理を追加</li>
</ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="WaveController.cs" data-lang="C#"><code>        // ウェーブが無い、もしくは敵が居なくなった場合
        if (currentWave == null || currentWave.transform.childCount == 0)
        {
            // ウェーブ作成
            currentWave = Instantiate(waves[nextWaveIndex]);
            // 次のウェーブ位置を計算
            nextWaveIndex++;
            // ウェーブ総数以上になったら繰り返すように余りを求める
            nextWaveIndex %= waves.Count;
        }
</code></pre></div>



<p>動ける範囲の外に出たオブジェクトを削除する処理を追加します。<br>弾は削除すると発射されなくなるので、カメラの後ろに隠すようにします。<br>弾かどうかはHitスクリプトが有るか無いかで判断しています。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>Update関数の次に以下の関数を追加</li>
</ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="WaveController.cs" data-lang="C#"><code>    // トリガーの外に出たら処理
    void OnTriggerExit2D(Collider2D obj)
    {
        // Hitスクリプトを持っていなかったら弾と判断
        Hit hit = obj.gameObject.GetComponent&lt;Hit&gt;();
        if (hit == null)
        {
            // カメラの後ろに隠す
            obj.gameObject.transform.position = hidePos;
        }
        else
        {
            // 削除
            Destroy(obj.gameObject);
        }
    }
</code></pre></div>



<p>スクリプトを保存してビルドします。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>CTRLを押しながらSキーを押してスクリプト保存</li>



<li>Unityエディタをクリックしてビルド</li>
</ol>



<h3 class="wp-block-heading"><span id="toc11">▷敵ウェーブ配置</span></h3>



<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-image size-full is-resized"><img decoding="async" width="418" height="285" src="https://d9m.one/wp-content/uploads/2022/09/SideSTG_055.png" alt="敵ウェーブが繰り返し出現する" class="wp-image-1567" style="aspect-ratio:314/214" srcset="https://d9m.one/wp-content/uploads/2022/09/SideSTG_055.png 418w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_055-300x205.png 300w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_055-130x90.png 130w" sizes="(max-width: 418px) 100vw, 418px" /><figcaption class="wp-element-caption">敵ウェーブが繰り返し出現する</figcaption></figure>
</div>



<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow"></div>
</div>



<p>・解説</p>



<p>敵ウェーブが順に表示されるように設定します。</p>



<p>・手順</p>



<p>WaveControllerのWavesに敵ウェーブをABCDの順で登録します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>ヒエラルキーの「WaveController」をクリック</li>



<li>プロジェクトの「Assets&gt;waveA」をインスペクターの「Wave Controller (スクリプト)&gt;Waves」にドラッグ＆ドロップ</li>



<li>プロジェクトの「Assets&gt;waveB」をインスペクターの「Wave Controller (スクリプト)&gt;Waves」にドラッグ＆ドロップ</li>



<li>プロジェクトの「Assets&gt;waveC」をインスペクターの「Wave Controller (スクリプト)&gt;Waves」にドラッグ＆ドロップ</li>



<li>プロジェクトの「Assets&gt;waveD」をインスペクターの「Wave Controller (スクリプト)&gt;Waves」にドラッグ＆ドロップ</li>
</ol>



<p>プロジェクトを保存して動作を確認します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>CTRLを押しながらSキーを押してプロジェクト保存</li>



<li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー開始</li>



<li>各種ウェーブが順番に登場することを確認</li>



<li>waveDが終了したらwaveAに戻ってループすることを確認</li>



<li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー終了</li>
</ol>



<h2 class="wp-block-heading"><span id="toc12">●完成</span></h2>



<p>シューティングとして遊べる形になりました。</p>



<p>次回はスコア制御を作って完成とします。<br>ゲームオーバーやタイトル、クリア等は気が向いたら作ります。</p>
<p>投稿 <a href="https://d9m.one/sidestg5/">敵ウェーブ～横スクロールシューティング⑤</a> は <a href="https://d9m.one">だくまたゲーム制作ブログ</a> に最初に表示されました。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://d9m.one/sidestg5/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>隕石と背景～横スクロールシューティング④</title>
		<link>https://d9m.one/sidestg4/</link>
					<comments>https://d9m.one/sidestg4/#respond</comments>
		
		<dc:creator><![CDATA[だくまた]]></dc:creator>
		<pubDate>Fri, 09 Sep 2022 10:48:20 +0000</pubDate>
				<category><![CDATA[Unity]]></category>
		<category><![CDATA[チュートリアル]]></category>
		<guid isPermaLink="false">https://d9m.one/?p=1411</guid>

					<description><![CDATA[<p>目次 ●はじめに●隕石作成▷隕石の作成▷隕石をランダムに動かす▷隕石大を破壊したら隕石小にする●背景▷背景表示▷背景スクロール●不具合修正▷現象確認▷衝突条件修正▷エフェクトが表示されない不具合 ●はじめに 2D横スクロ [&#8230;]</p>
<p>投稿 <a href="https://d9m.one/sidestg4/">隕石と背景～横スクロールシューティング④</a> は <a href="https://d9m.one">だくまたゲーム制作ブログ</a> に最初に表示されました。</p>
]]></description>
										<content:encoded><![CDATA[

  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-8" checked><label class="toc-title" for="toc-checkbox-8">目次</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">●はじめに</a></li><li><a href="#toc2" tabindex="0">●隕石作成</a><ol><li><a href="#toc3" tabindex="0">▷隕石の作成</a></li><li><a href="#toc4" tabindex="0">▷隕石をランダムに動かす</a></li><li><a href="#toc5" tabindex="0">▷隕石大を破壊したら隕石小にする</a></li></ol></li><li><a href="#toc6" tabindex="0">●背景</a><ol><li><a href="#toc7" tabindex="0">▷背景表示</a></li><li><a href="#toc8" tabindex="0">▷背景スクロール</a></li></ol></li><li><a href="#toc9" tabindex="0">●不具合修正</a><ol><li><a href="#toc10" tabindex="0">▷現象確認</a></li><li><a href="#toc11" tabindex="0">▷衝突条件修正</a></li><li><a href="#toc12" tabindex="0">▷エフェクトが表示されない不具合</a></li></ol></li></ol>
    </div>
  </div>

<h2 class="wp-block-heading"><span id="toc1">●はじめに</span></h2>



<p>2D横スクロールシューティング作成の４回目になります。<br>アセットストアで無料公開されている「<a rel="noopener" href="https://assetstore.unity.com/packages/2d/characters/warped-space-shooter-181590#content" target="_blank">Warped Space Shooter</a>」をお借りして作成しています。</p>



<p>今回は「分裂する隕石と背景」を作成します。<br>手順が長くなったのと、作成ペースが上がらないのでスコア、敵ウェーブはさらに後回しにします。</p>



<iframe loading="lazy" src="/game/sidestg4/index.html" width="780" height="465" scrolling="no" frameborder="0"></iframe>



<p>　↑これが完成します。</p>



<h2 class="wp-block-heading"><span id="toc2">●隕石作成</span></h2>



<h3 class="wp-block-heading"><span id="toc3">▷隕石の作成</span></h3>



<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/09/SideSTG_032.png" alt="" class="wp-image-1421" width="521" height="629" srcset="https://d9m.one/wp-content/uploads/2022/09/SideSTG_032.png 695w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_032-249x300.png 249w" sizes="(max-width: 521px) 100vw, 521px" /><figcaption>隕石のインスペクター情報</figcaption></figure>
</div>



<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/09/SideSTG_031.png" target="_blank"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/09/SideSTG_031.png" alt="" class="wp-image-1420" width="314" height="214" srcset="https://d9m.one/wp-content/uploads/2022/09/SideSTG_031.png 418w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_031-300x205.png 300w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_031-130x90.png 130w" sizes="(max-width: 314px) 100vw, 314px" /></a><figcaption>隕石を制御</figcaption></figure>
</div>
</div>



<p>・解説</p>



<p>隕石の画像２枚をそれぞれ配置し、それぞれを敵として作成してプレハブ化します。<br>衝突させるためHitスクリプトを割り当て、小さい方をEnergy(1)、大きい方をEnergy(5)とします。<br>Rigidbody2Dを割り当てて重力スケールを(0)、衝突判定を(連続的)とし、CircleCollider2Dを割り当ててトリガーとします。</p>



<p>・隕石小の手順</p>



<p>隕石画像小を配置します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>「Assets&gt;Space Shooter Assets&gt;Artwork&gt;asteroids&gt;asteroid-small」をシーンにドラッグ＆ドロップ</li><li>インスペクターの「Transform&gt;スケール」に(4, 4, 0)を入力</li></ol>



<p>Hitスクリプトを割り当てます。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>プロジェクトの「Assets>Hit」をヒエラルキーの「asteroid-small」にドラッグ＆ドロップ</li><li>インスペクターの「Hit (スクリプト)>Energy」に(1)を入力</li></ol>



<p>Rigidbody 2Dを割り当てます。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>インスペクターの「コンポーネントを追加」をクリック</li><li>「Physics 2D&gt;Rigidbody 2D」をクリック</li><li>インスペクターの「Rigidbody 2D&gt;重力スケール」に(0)を入力</li><li>インスペクターの「Rigidbody 2D&gt;衝突判定」を(連続的)に変更</li></ol>



<p>Circle Collider 2Dを割り当てます。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>インスペクターの「コンポーネントを追加」をクリック</li><li>「Physics 2D&gt;Circle Collider 2D」をクリック</li><li>インスペクターの「Circle Collider 2D&gt;トリガーにする」をチェック</li></ol>



<p>Enemyスクリプトを割り当てます。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>プロジェクトの「Assets>Enemy」をヒエラルキーの「asteroid-small」にドラッグ＆ドロップ</li><li>インスペクターの「Enemy (スクリプト)>速度」に(1)を入力</li><li>インスペクターの「Enemy (スクリプト)>Cycle Count」に(0)を入力</li><li>インスペクターの「Enemy (スクリプト)>Curve Length」に(0)を入力</li></ol>



<p>プレハブ化します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>ヒエラルキーの「asteroid-small」をプロジェクトの「Assets」にドラッグ＆ドロップ</li></ol>



<p>・隕石大の手順</p>



<p>隕石画像大を配置します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>「Assets&gt;Space Shooter Assets&gt;Artwork&gt;asteroids&gt;asteroid」をシーンにドラッグ＆ドロップ</li><li>インスペクターの「Transform&gt;スケール」に(4, 4, 0)を入力</li></ol>



<p>Hitスクリプトを割り当てます。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>プロジェクトの「Assets>Hit」をヒエラルキーの「asteroid」にドラッグ＆ドロップ</li><li>インスペクターの「Hit (スクリプト)>Energy」に(5)を入力</li></ol>



<p>Rigidbody 2Dを割り当てます。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>インスペクターの「コンポーネントを追加」をクリック</li><li>「Physics 2D&gt;Rigidbody 2D」をクリック</li><li>インスペクターの「Rigidbody 2D&gt;重力スケール」に(0)を入力</li><li>インスペクターの「Rigidbody 2D&gt;衝突判定」を(連続的)に変更</li></ol>



<p>Circle Collider 2Dを割り当てます。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>インスペクターの「コンポーネントを追加」をクリック</li><li>「Physics 2D&gt;Circle Collider 2D」をクリック</li><li>インスペクターの「Circle Collider 2D&gt;トリガーにする」をチェック</li></ol>



<p>Enemyスクリプトを割り当てます。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>「Assets&gt;Enemy」をヒエラルキーの「asteroid」にドラッグ＆ドロップ</li><li>インスペクターの「Enemy (スクリプト)&gt;速度」に(1)を入力</li><li>インスペクターの「Enemy (スクリプト)&gt;Cycle Count」に(0)を入力</li><li>インスペクターの「Enemy (スクリプト)&gt;Curve Length」に(0)を入力</li></ol>



<p>プレハブ化します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>ヒエラルキーの「asteroid」をプロジェクトの「Assets」にドラッグ＆ドロップ</li></ol>



<p>・動作確認</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー開始</li><li>隕石が画面左に向かって移動してくることを確認</li><li>隕石が破壊できることを確認</li><li>隕石に体当たりするとやられることを確認</li><li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー終了</li></ol>



<h3 class="wp-block-heading"><span id="toc4">▷隕石をランダムに動かす</span></h3>



<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/09/SideSTG_040.png" target="_blank"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/09/SideSTG_040.png" alt="" class="wp-image-1528" width="314" height="214" srcset="https://d9m.one/wp-content/uploads/2022/09/SideSTG_040.png 418w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_040-300x205.png 300w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_040-130x90.png 130w" sizes="(max-width: 314px) 100vw, 314px" /></a><figcaption>プレビューごとに隕石の進む方向が変わる</figcaption></figure>



<p>・解説</p>



<p>隕石をランダムな方向に動かすために変数を追加し、開始時の角度をランダムに設定します。</p>



<p>・手順</p>



<p>ランダム範囲を指定する変数を追加します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>プロジェクトの「Assets&gt;Enemy」をダブルクリック</li><li>「１秒間に進む距離」の下に以下の変数を追加</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Enemy.cs" data-lang="C#"><code>    public float randomMinDir = 0;  // ランダム方向の最小値
    public float randomMaxDir = 0;  // ランダム方向の最大値
</code></pre></div>



<p>ランダム値を設定する処理を追加します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>Start関数内の最後に以下の処理を追加</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Enemy.cs" data-lang="C#"><code>        // ランダム範囲が指定されている場合
        if (randomMinDir != randomMaxDir)
        {
            // ランダム方向を設定
            float rotZ = Random.Range(randomMinDir, randomMaxDir);
            transform.rotation = Quaternion.Euler(0, 0, rotZ);
        }
</code></pre></div>



<p>スクリプトを保存してビルドします。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>CTRLを押しながらSキーを押してスクリプト保存</li><li>Unityエディタをクリックしてビルド</li></ol>



<p>隕石大のランダム方向を指定します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>プロジェクトの「Assets&gt;asteroid-small」をダブルクリック</li><li>インスペクターの「Enemy (スクリプト)&gt;Random Max Dir」に(360)を入力</li><li>ヒエラルキーの左上にある「&lt;」をクリック</li><li>プロジェクトの「Assets&gt;asteroid」をダブルクリック</li><li>インスペクターの「Enemy (スクリプト)&gt;Random Max Dir」に(360)を入力</li><li>ヒエラルキーの左上にある「&lt;」をクリック</li></ol>



<p>動作を確認します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー開始</li><li>プレビューを繰り返し、隕石がランダムな方向に動くことを確認</li><li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー終了</li></ol>



<h3 class="wp-block-heading"><span id="toc5">▷隕石大を破壊したら隕石小にする</span></h3>



<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/09/SideSTG_033.png" target="_blank"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/09/SideSTG_033.png" alt="" class="wp-image-1423" width="275" height="461" srcset="https://d9m.one/wp-content/uploads/2022/09/SideSTG_033.png 367w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_033-179x300.png 179w" sizes="(max-width: 275px) 100vw, 275px" /></a><figcaption>隕石大のインスペクター情報</figcaption></figure>
</div>



<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/09/SideSTG_034.png" target="_blank"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/09/SideSTG_034.png" alt="" class="wp-image-1424" width="314" height="214" srcset="https://d9m.one/wp-content/uploads/2022/09/SideSTG_034.png 418w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_034-300x205.png 300w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_034-130x90.png 130w" sizes="(max-width: 314px) 100vw, 314px" /></a><figcaption>隕石大を破壊したら隕石小になる</figcaption></figure>
</div>
</div>



<p>・解説</p>



<p>爆発後に作成するプレハブを指定できるよう、Enemyスクリプトを修正します。</p>



<p>・手順</p>



<p>爆発後に作成するプレハブ一覧を保存するため、変数を追加します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>プロジェクトの「Assets&gt;Hit」をダブルクリック</li><li>Start関数の前に以下の変数を追加</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Hit.cs" data-lang="C#"><code>    // 爆発後に作成するプレハブの一覧
    public List&lt;GameObject&gt; leavePrefabs;
    // 爆発後に作成したゲームオブジェクトの相対位置
    public List&lt;Vector3&gt; leavePostions;
</code></pre></div>



<p>爆発時にプレハブを残す処理を追加します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>OnTriggerEnter2D内の「自分を削除」する処理の前に以下の処理を追加</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Hit.cs" data-lang="C#"><code>            // プレハブを残す
            Vector3 pos = transform.position;
            for (int i = 0; i &lt; leavePrefabs.Count; i++)
            {
                Instantiate(leavePrefabs[i], pos + leavePostions[i], Quaternion.identity);
            }
</code></pre></div>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>CTRLを押しながらSキーを押してスクリプト保存</li><li>Unityエディタをクリックしてビルド</li></ol>



<p>爆発時に作成するプレハブを割り当てます。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>ヒエラルキーの「asteroid」をクリック</li><li>プロジェクトの「Assets&gt;asteroid-small」をインスペクターの「Hit (スクリプト)&gt;Leave Prefabs」にドラッグ＆ドロップ</li><li>プロジェクトの「Assets&gt;asteroid-small」をインスペクターの「Hit (スクリプト)&gt;Leave Prefabs」にドラッグ＆ドロップ</li><li>プロジェクトの「Assets&gt;asteroid-small」をインスペクターの「Hit (スクリプト)&gt;Leave Prefabs」にドラッグ＆ドロップ</li></ol>



<p>爆発時に作成したプレハブの相対位置を設定します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>インスペクターの「Hit (スクリプト)>Leave Positions」右に(3)を入力</li><li>インスペクターの「Hit (スクリプト)>Leave Positions」左にある▶をクリックして展開</li><li>インスペクターの「Hit (スクリプト)>Leave Positions>要素0>Y」に(0.5)を入力</li><li>インスペクターの「Hit (スクリプト)>Leave Positions>要素1」に(-0.3, -0.3, 0)を入力</li><li>インスペクターの「Hit (スクリプト)>Leave Positions>要素1」に(0.3, -0.3, 0)を入力</li></ol>



<p>隕石大の変更をプレハブに反映します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>インスペクターの右上にある「オーバーライド」をクリックし、「すべてを適用する」をクリック</li></ol>



<p>プロジェクトを保存して、</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>CTRLを押しながらSキーを押してプロジェクト保存</li></ol>



<p>動作を確認します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー開始</li><li>隕石大を破壊したら隕石小が３つ出現することを確認</li><li>残された隕石小を破壊できることを確認</li><li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー終了</li></ol>



<h2 class="wp-block-heading"><span id="toc6">●背景</span></h2>



<h3 class="wp-block-heading"><span id="toc7">▷背景表示</span></h3>



<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/09/SideSTG_035.png" target="_blank"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/09/SideSTG_035.png" alt="" class="wp-image-1519" width="184" height="310" srcset="https://d9m.one/wp-content/uploads/2022/09/SideSTG_035.png 245w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_035-178x300.png 178w" sizes="(max-width: 184px) 100vw, 184px" /></a><figcaption>ヒエラルキーの状態</figcaption></figure>
</div>



<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/09/SideSTG_036.png" target="_blank"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/09/SideSTG_036.png" alt="" class="wp-image-1520" width="314" height="214" srcset="https://d9m.one/wp-content/uploads/2022/09/SideSTG_036.png 418w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_036-300x205.png 300w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_036-130x90.png 130w" sizes="(max-width: 314px) 100vw, 314px" /></a><figcaption>BGMのインスペクター情報</figcaption></figure>
</div>
</div>



<p>・解説</p>



<p>背景スクロールを作成します。</p>



<p>前後関係と多重スクロールを制御しやすいように空オブジェクトで整理し、<br>空オブジェクトの中に背景画像を配置していきます。</p>



<p>・手順</p>



<p>背景画像を配置するための空オブジェクトを作成します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>ヒエラルキーの左上にある「＋」をクリックし、「空のオブジェクトを作成」をクリック</li><li>「background」と入力してENTERを押す</li><li>インスペクターの「Transform>位置」に(0, 0, 0)を入力</li><li>ヒエラルキーの「background」を右クリックし、「空のオブジェクトを作成」をクリック</li><li>「back」と入力してENTERを押す</li><li>インスペクターの「Transform>スケール」に(6.6, 6.6, 1)を入力</li><li>ヒエラルキーの「background」を右クリックし、「空のオブジェクトを作成」をクリック</li><li>「stars」と入力してENTERを押す</li><li>インスペクターの「Transform>スケール」に(6.6, 6.6, 1)を入力</li><li>ヒエラルキーの「background」を右クリックし、「空のオブジェクトを作成」をクリック</li><li>「planet」と入力してENTERを押す</li><li>インスペクターの「Transform>スケール」に(6.6, 6.6, 1)を入力</li></ol>



<p>最も奥の背景画像を表示します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>プロジェクトの「Assets>Space Shooter Assets>Artwork>background>bg-back」をヒエラルキーの「background>back」にドラッグ＆ドロップ</li><li>インスペクターの「Transform>スケール」に(1, 1, 1)を入力</li><li>インスペクターの「Sprite Renderer>追加設定>レイヤーの順序」に(-100)を入力</li><li>ヒエラルキーの「background>back>bg-stars」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターの「Transform>位置>X」に(2.72)を入力</li><li>ヒエラルキーの「background>back>bg-back (1)」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターの「Transform>位置>X」に(5.44)を入力</li></ol>



<p>中央の背景画像を表示します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>プロジェクトの「Assets>Space Shooter Assets>Artwork>background>bg-stars」をヒエラルキーの「background>stars」にドラッグ＆ドロップ</li><li>インスペクターの「Transform>スケール」に(1, 1, 1)を入力</li><li>インスペクターの「Sprite Renderer>追加設定>レイヤーの順序」に(-60)を入力</li><li>ヒエラルキーの「background>stars>bg-stars」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターの「Transform>位置>X」に(2.72)を入力</li><li>インスペクターの「Sprite Renderer>反転>Y」にチェックを入れる</li><li>ヒエラルキーの「background>back>bg-stars (1)」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターの「Transform>位置>X」に(5.44)を入力</li><li>インスペクターの「Sprite Renderer>反転>Y」のチェックを外す</li></ol>



<p>手前の背景画像を表示します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>プロジェクトの「Assets>Space Shooter Assets>Artwork>background>bg-planet」をヒエラルキーの「background>planet」にドラッグ＆ドロップ</li><li>インスペクターの「Transform>スケール」に(1, 1, 1)を入力</li><li>インスペクターの「Sprite Renderer>追加設定>レイヤーの順序」に(-20)を入力</li><li>ヒエラルキーの「background>back>bg-planet」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターの「Transform>位置>X」に(2.72)を入力</li><li>インスペクターの「Sprite Renderer>反転>Y」にチェックを入れる</li><li>ヒエラルキーの「background>back>bg-planet(1)」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターの「Transform>位置>X」に(5.44)を入力</li><li>インスペクターの「Sprite Renderer>反転>Y」のチェックを外す</li></ol>



<h3 class="wp-block-heading"><span id="toc8">▷背景スクロール</span></h3>



<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/09/SideSTG_037.png" target="_blank"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/09/SideSTG_037.png" alt="" class="wp-image-1521" width="314" height="214" srcset="https://d9m.one/wp-content/uploads/2022/09/SideSTG_037.png 418w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_037-300x205.png 300w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_037-130x90.png 130w" sizes="(max-width: 314px) 100vw, 314px" /></a><figcaption>背景が多重スクロールする</figcaption></figure>



<p>・解説</p>



<p>背景をスクロールさせるためのスクリプトを作成します。</p>



<p>・手順</p>



<p>スクリプト「Background」を作成します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>プロジェクトの「Assets」を右クリックし、「作成>C# スクリプト」をクリック</li><li>「Background」と入力してENTERを押す</li><li>プロジェクトの「Assets>Background」をダブルクリック</li><li>Start関数の前にスクロール制御する変数を追加</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Background.cs" data-lang="C#"><code>    public float speed = 0.01f;             // １フレームに動く距離
    public float scrollWidth = 5.44f;       // ループ幅
</code></pre></div>



<p>自分のスケールに合わせて「ループの幅」を計算する処理を記述します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>Start関数に以下の処理を追加</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Background.cs" data-lang="C#"><code>        // 自分のスケールに合わせて「ループの幅」を計算
        scrollWidth *= transform.localScale.x;
</code></pre></div>



<p>ループしてスクロールし続けるように処理を記述します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>Update関数の次に以下の関数を追加</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Background.cs" data-lang="C#"><code>    // 物理更新時に処理
    void FixedUpdate()
    {
        // スクロール
        Vector3 pos = transform.position;
        pos.x -= speed;
        // ループ幅を超えて移動した場合
        if (-scrollWidth &gt; pos.x)
        {
            // ループ幅分戻す
            pos.x += scrollWidth;
        }
        transform.position = pos;
    }
</code></pre></div>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>CTRLを押しながらSキーを押してスクリプト保存</li><li>Unityエディタをクリックしてビルド</li></ol>



<p>スクリプトを各背景ゲームオブジェクトに割り当て、速度を設定します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>ヒエラルキーの「background>back」にプロジェクトの「Assets>Background」をドラッグ＆ドロップ</li><li>ヒエラルキーの「background>back」をクリック</li><li>インスペクターの「Background (スクリプト)>速度」に(0.005)を入力</li><li>ヒエラルキーの「background>stars」にプロジェクトの「Assets>Background」をドラッグ＆ドロップ</li><li>ヒエラルキーの「background>planet」にプロジェクトの「Assets>Background」をドラッグ＆ドロップ</li><li>ヒエラルキーの「background>planet」をクリック</li><li>インスペクターの「Background (スクリプト)>速度」に(0.02)を入力</li></ol>



<p>プロジェクトを保存して、</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>CTRLを押しながらSキーを押してプロジェクト保存</li></ol>



<p>動作を確認します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー開始</li><li>背景画像の３つが多重スクロールすることを確認</li><li>背景画像がループしてスクロールすることを確認</li><li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー終了</li></ol>



<h2 class="wp-block-heading"><span id="toc9">●不具合修正</span></h2>



<h3 class="wp-block-heading"><span id="toc10">▷現象確認</span></h3>



<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/09/SideSTG_038.png" target="_blank"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/09/SideSTG_038.png" alt="" class="wp-image-1523" width="314" height="214" srcset="https://d9m.one/wp-content/uploads/2022/09/SideSTG_038.png 418w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_038-300x205.png 300w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_038-130x90.png 130w" sizes="(max-width: 314px) 100vw, 314px" /></a><figcaption>開始後にすぐ破壊される不具合</figcaption></figure>
</div>



<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/09/SideSTG_039.png" target="_blank"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/09/SideSTG_039.png" alt="" class="wp-image-1524" width="314" height="214" srcset="https://d9m.one/wp-content/uploads/2022/09/SideSTG_039.png 418w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_039-300x205.png 300w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_039-130x90.png 130w" sizes="(max-width: 314px) 100vw, 314px" /></a><figcaption>修正後、弾が当たるまで破壊されなくなる</figcaption></figure>
</div>
</div>



<p>・解説</p>



<p>弾が見えない状態でも敵に命中していましたので修正します。</p>



<p>・手順</p>



<p>修正後に現象が発生しなくなる事を確認するため、一旦不具合を確認します。<br>自機の前方に隕石大を配置します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>ヒエラルキーの「asteroid」をクリック</li><li>インスペクターの「Transform>位置」に(6, 0, 0)を入力</li><li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー開始</li><li>弾が当たる前に隕石大が破壊されることを確認</li><li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー終了</li></ol>



<h3 class="wp-block-heading"><span id="toc11">▷衝突条件修正</span></h3>



<p>・解説</p>



<p>Debug.Logで隕石に衝突したオブジェクトの位置を確認したところ、見えないZ位置にある弾が衝突していました。<br>特に深く考えてませんでしたが、2Dの衝突チェックなんだから無視されて当然でした。</p>



<p>・手順</p>



<p>Z位置が0以外の場合、衝突しないようにします。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>プロジェクトの「Assets>Hit」をダブルクリック</li><li>OnTriggerEnter2Dの「タグが同じ場合は衝突させない」処理の次に以下の処理を追加</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Background.cs" data-lang="C#"><code>        // Z軸が0以外の場合は衝突させない
        if (collider.gameObject.transform.position.z != 0)
        {
            return;
        }
</code></pre></div>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>CTRLを押しながらSキーを押してスクリプト保存</li><li>Unityエディタをクリックしてビルド</li></ol>



<p>プロジェクトを保存して、</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>CTRLを押しながらSキーを押してプロジェクト保存</li></ol>



<p>動作を確認します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー開始</li><li>弾が隕石台に命中してから破壊されることを確認</li><li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー終了</li></ol>



<h3 class="wp-block-heading"><span id="toc12">▷エフェクトが表示されない不具合</span></h3>



<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/09/SideSTG_041.png" target="_blank"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/09/SideSTG_041.png" alt="" class="wp-image-1530" width="221" height="277" srcset="https://d9m.one/wp-content/uploads/2022/09/SideSTG_041.png 294w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_041-239x300.png 239w" sizes="(max-width: 221px) 100vw, 221px" /></a><figcaption>命中エフェクトのインスペクター情報</figcaption></figure>
</div>



<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/09/SideSTG_042.png" target="_blank"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/09/SideSTG_042.png" alt="" class="wp-image-1531" width="221" height="277" srcset="https://d9m.one/wp-content/uploads/2022/09/SideSTG_042.png 294w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_042-239x300.png 239w" sizes="(max-width: 221px) 100vw, 221px" /></a><figcaption>爆発エフェクトのインスペクター情報</figcaption></figure>
</div>
</div>



<p>・解説</p>



<p>プレビューすると表示されない時がありました。<br>１００％の発生ではないので確認が難しいですが…。<br>恐らくエフェクトとキャラクターの表示順が同じために発生しているので、はっきり表示順を指定します。</p>



<p>・手順</p>



<p>命中エフェクトの「レイヤーの順序」を大きくして手前に出します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>プロジェクトの「Assets>hit1」をダブルクリック</li><li>インスペクターの「Sprite Renderer>追加設定>レイヤーの順序」に(10)を入力</li><li>ヒエラルキーの左上にある「＜」をクリック</li></ol>



<p>同じように、爆発エフェクトの「レイヤーの順序」を大きくして手前に出します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>プロジェクトの「Assets>explosion1」をダブルクリック</li><li>インスペクターの「Sprite Renderer>追加設定>レイヤーの順序」に(10)を入力</li><li>ヒエラルキーの左上にある「＜」をクリック</li></ol>



<p>動作を確認します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー開始</li><li>プレビューし、エフェクトが手前に表示されることを確認</li><li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー終了</li></ol>



<p><strong>●完成</strong></p>



<p>隕石と背景を追加し、遊べそうな雰囲気が出てきました。</p>



<p>次回はいくつか敵ウェーブを作成したいと思います。<br>敵グループを順番に出現させ、ループさせて遊べる形にします。</p>
<p>投稿 <a href="https://d9m.one/sidestg4/">隕石と背景～横スクロールシューティング④</a> は <a href="https://d9m.one">だくまたゲーム制作ブログ</a> に最初に表示されました。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://d9m.one/sidestg4/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>オーディオ～横スクロールシューティング③</title>
		<link>https://d9m.one/sidestg3/</link>
					<comments>https://d9m.one/sidestg3/#respond</comments>
		
		<dc:creator><![CDATA[だくまた]]></dc:creator>
		<pubDate>Fri, 02 Sep 2022 15:58:19 +0000</pubDate>
				<category><![CDATA[Unity]]></category>
		<category><![CDATA[チュートリアル]]></category>
		<guid isPermaLink="false">https://d9m.one/?p=1350</guid>

					<description><![CDATA[<p>目次 ●はじめに●自機のやられ▷自機の衝突設定▷弾を加速度で動かす▷タグで衝突を制御●オーディオ制御▷BGM設定▷効果音再生制御●完成 ●はじめに 今回のメインはオーディオです。 2D横スクロールシューティング作成の３回 [&#8230;]</p>
<p>投稿 <a href="https://d9m.one/sidestg3/">オーディオ～横スクロールシューティング③</a> は <a href="https://d9m.one">だくまたゲーム制作ブログ</a> に最初に表示されました。</p>
]]></description>
										<content:encoded><![CDATA[

  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-10" checked><label class="toc-title" for="toc-checkbox-10">目次</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">●はじめに</a></li><li><a href="#toc2" tabindex="0">●自機のやられ</a><ol><li><a href="#toc3" tabindex="0">▷自機の衝突設定</a></li><li><a href="#toc4" tabindex="0">▷弾を加速度で動かす</a></li><li><a href="#toc5" tabindex="0">▷タグで衝突を制御</a></li></ol></li><li><a href="#toc6" tabindex="0">●オーディオ制御</a><ol><li><a href="#toc7" tabindex="0">▷BGM設定</a></li><li><a href="#toc8" tabindex="0">▷効果音再生制御</a></li></ol></li><li><a href="#toc9" tabindex="0">●完成</a></li></ol>
    </div>
  </div>

<h2 class="wp-block-heading"><span id="toc1">●はじめに</span></h2>



<p>今回のメインはオーディオです。</p>



<p>2D横スクロールシューティング作成の３回目になります。<br>アセットストアで無料公開されている「<a rel="noopener" href="https://assetstore.unity.com/packages/2d/characters/warped-space-shooter-181590#content" target="_blank">Warped Space Shooter</a>」をお借りして作成しています。</p>



<p>「自機のやられ、BGM再生、効果音再生」を作成し、シューティングっぽくなるようにします。</p>



<iframe loading="lazy" src="/game/sidestg3/index.html" width="780" height="465" scrolling="no" frameborder="0"></iframe>



<p>　↑これが完成する。</p>



<h2 class="wp-block-heading"><span id="toc2">●自機のやられ</span></h2>



<h3 class="wp-block-heading"><span id="toc3">▷自機の衝突設定</span></h3>



<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-image size-full is-resized"><img decoding="async" width="277" height="709" src="https://d9m.one/wp-content/uploads/2022/09/SideSTG_024.png" alt="player1のインスペクター情報" class="wp-image-1394" style="aspect-ratio:208/532" srcset="https://d9m.one/wp-content/uploads/2022/09/SideSTG_024.png 277w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_024-117x300.png 117w" sizes="(max-width: 277px) 100vw, 277px" /><figcaption class="wp-element-caption">player1のインスペクター情報</figcaption></figure>
</div>



<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/09/SideSTG_021.png" target="_blank"><img decoding="async" width="418" height="285" src="https://d9m.one/wp-content/uploads/2022/09/SideSTG_021.png" alt="体当たりで自機が消える、そしてバグ" class="wp-image-1391" style="aspect-ratio:314/214" srcset="https://d9m.one/wp-content/uploads/2022/09/SideSTG_021.png 418w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_021-300x205.png 300w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_021-130x90.png 130w" sizes="(max-width: 418px) 100vw, 418px" /></a><figcaption class="wp-element-caption">体当たりで自機が消える、そしてバグ</figcaption></figure>
</div>
</div>



<p>・解説</p>



<p>Hitスクリプトを設定し、１撃でやられるようEnergyに(1)を設定します。<br>Rigidbody2Dを割り当てて重力スケールを(0)、衝突判定を(連続的)とし、<br>CircleCollider2Dを割り当ててオフセットXを(0.05)、半径を(0.05)とします。</p>



<p>・手順</p>



<p>スクリプトを割り当てます。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>「Assets&gt;Hit」をヒエラルキーの「Player1」にドラッグ＆ドロップ</li>



<li>インスペクターの「Hit (スクリプト)&gt;Energy」に(1)を入力</li>
</ol>



<p>Rigidbody 2Dを割り当てます。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>インスペクターの「コンポーネントを追加」をクリック</li>



<li>「Physics 2D&gt;Rigidbody 2D」をクリック</li>



<li>インスペクターの「Rigidbody 2D&gt;重力スケール」に(0)を入力</li>



<li>インスペクターの「Rigidbody 2D&gt;衝突判定」を(連続的)に変更</li>
</ol>



<p>Circle Collider 2Dを割り当てます。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>インスペクターの「コンポーネントを追加」をクリック</li>



<li>「Physics 2D&gt;Circle Collider 2D」をクリック</li>



<li>インスペクターの「Circle Collider 2D&gt;トリガーにする」をチェック</li>



<li>インスペクターの「Circle Collider 2D&gt;オフセット&gt;X」に(0.05)を入力</li>



<li>インスペクターの「Circle Collider 2D&gt;半径」を(0.05)に変更</li>
</ol>



<p>・動作確認</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー開始</li>



<li>敵に体当たりしたら自機が爆発することを確認</li>



<li>自機が爆発したら自機の弾が停止するバグが発生することを確認…</li>



<li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー終了</li>
</ol>



<h3 class="wp-block-heading"><span id="toc4">▷弾を加速度で動かす</span></h3>



<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-image size-full is-resized"><img decoding="async" width="277" height="639" src="https://d9m.one/wp-content/uploads/2022/09/SideSTG_025.png" alt="shoot1プレハブのインスペクター情報" class="wp-image-1396" style="aspect-ratio:208/479" srcset="https://d9m.one/wp-content/uploads/2022/09/SideSTG_025.png 277w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_025-130x300.png 130w" sizes="(max-width: 277px) 100vw, 277px" /><figcaption class="wp-element-caption">shoot1プレハブのインスペクター情報</figcaption></figure>
</div>



<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/09/SideSTG_022.png" target="_blank"><img decoding="async" width="418" height="285" src="https://d9m.one/wp-content/uploads/2022/09/SideSTG_022.png" alt="自機がやられても弾が止まらない" class="wp-image-1392" style="aspect-ratio:314/214" srcset="https://d9m.one/wp-content/uploads/2022/09/SideSTG_022.png 418w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_022-300x205.png 300w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_022-130x90.png 130w" sizes="(max-width: 418px) 100vw, 418px" /></a><figcaption class="wp-element-caption">自機がやられても弾が止まらない</figcaption></figure>
</div>
</div>



<p>・解説</p>



<p>弾が停止しないよう弾にRigidbody2Dを割り当てて加速度を与え、自分で動くようにします。</p>



<p>・手順</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>プロジェクトの「Assets&gt;Player」をダブルクリック</li>



<li>Start関数の繰り返し処理内にある「プレハブから弾を作成」の処理の後に以下の処理を追加</li>
</ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Player.cs" data-lang="C#"><code>            // リジッドボディの加速度を指定
            Rigidbody2D rb = shotList[i].GetComponent&lt;Rigidbody2D&gt;();
            rb.velocity = transform.right * shotSpeed;
</code></pre></div>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>FixedUpdateの最後にある「弾移動（どうせ見えないので全部動かす）」処理を削除</li>
</ol>



<p>スクリプトを保存してビルドします。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>CTRLを押しながらSキーを押してスクリプト保存</li>



<li>Unityエディタをクリックしてビルド</li>
</ol>



<p>弾にRigidbody 2Dを設定します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>プロジェクトの「Assets&gt;shoot1」をダブルクリック</li>



<li>インスペクターの「コンポーネントを追加」をクリック</li>



<li>「Physics 2D&gt;Rigidbody 2D」をクリック</li>



<li>インスペクターの「Rigidbody 2D&gt;重力スケール」に(0)を入力</li>



<li>インスペクターの「Rigidbody 2D&gt;衝突判定」を(連続的)に変更</li>



<li>ヒエラルキー左上の「&lt;」をクリックしてプレハブ編集を終了</li>
</ol>



<p>動作を確認します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー開始</li>



<li>敵に体当たりして爆発しても弾が停止しないことを確認</li>



<li>自機の弾に体当たりしても爆発することを確認</li>



<li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー終了</li>
</ol>



<h3 class="wp-block-heading"><span id="toc5">▷タグで衝突を制御</span></h3>



<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/09/SideSTG_026.png" target="_blank"><img decoding="async" width="277" height="355" src="https://d9m.one/wp-content/uploads/2022/09/SideSTG_026.png" alt="自機の弾に当たっても爆発しない" class="wp-image-1397" style="aspect-ratio:208/266" srcset="https://d9m.one/wp-content/uploads/2022/09/SideSTG_026.png 277w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_026-234x300.png 234w" sizes="(max-width: 277px) 100vw, 277px" /></a><figcaption class="wp-element-caption">自機の弾に当たっても爆発しない</figcaption></figure>
</div>



<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/09/SideSTG_023.png" target="_blank"><img decoding="async" width="418" height="285" src="https://d9m.one/wp-content/uploads/2022/09/SideSTG_023.png" alt="自機の弾に当たっても爆発しない" class="wp-image-1393" style="aspect-ratio:314/214" srcset="https://d9m.one/wp-content/uploads/2022/09/SideSTG_023.png 418w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_023-300x205.png 300w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_023-130x90.png 130w" sizes="(max-width: 418px) 100vw, 418px" /></a><figcaption class="wp-element-caption">自機の弾に当たっても爆発しない</figcaption></figure>
</div>
</div>



<p>・解説</p>



<p>自機の弾は誰と衝突しても破壊できる状態です。<br>自機と自機の弾にPlayerタグを割り当て、自分と同じタグの場合は衝突しないようスクリプトを修正します。</p>



<p>・手順</p>



<p>自機のタグを「Player」にします。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>ヒエラルキーの「Player1」をクリック</li>



<li>インスペクターのタグを「Player」に変更</li>
</ol>



<p>自機の弾のタグを「Player」にします。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>プロジェクトの「Assets&gt;shoot1」をダブルクリック</li>



<li>インスペクターのタグを「Player」に変更</li>



<li>ヒエラルキー左上の「&lt;」をクリックしてプレハブ編集を終了</li>
</ol>



<p>タグが同じ場合は衝突しないように条件を追加します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>プロジェクトの「Assets&gt;Hit」をダブルクリック</li>



<li>OnTriggerEnter2D関数の先頭に以下の処理を追加</li>
</ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Hit.cs" data-lang="C#"><code>        // タグが同じ場合は衝突させない
        if (tag == collider.gameObject.tag) {
            return;
        }
</code></pre></div>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>CTRLを押しながらSキーを押してスクリプト保存</li>



<li>Unityエディタをクリックしてビルド</li>
</ol>



<p>プロジェクトを保存して、</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>CTRLを押しながらSキーを押してプロジェクト保存</li>
</ol>



<p>動作を確認します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー開始</li>



<li>敵に弾を当てて倒せることを確認</li>



<li>自分の弾に当たっても爆発しないことを確認</li>



<li>再度プレビューし、敵への体当たりで爆発することを確認</li>



<li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー終了</li>
</ol>



<h2 class="wp-block-heading"><span id="toc6">●オーディオ制御</span></h2>



<h3 class="wp-block-heading"><span id="toc7">▷BGM設定</span></h3>



<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/09/SideSTG_028.png" target="_blank"><img decoding="async" width="294" height="491" src="https://d9m.one/wp-content/uploads/2022/09/SideSTG_028.png" alt="オーディオインポート設定" class="wp-image-1400" style="aspect-ratio:221/368" srcset="https://d9m.one/wp-content/uploads/2022/09/SideSTG_028.png 294w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_028-180x300.png 180w" sizes="(max-width: 294px) 100vw, 294px" /></a><figcaption class="wp-element-caption">オーディオのインポート設定</figcaption></figure>
</div>



<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/09/SideSTG_027.png" target="_blank"><img decoding="async" width="294" height="544" src="https://d9m.one/wp-content/uploads/2022/09/SideSTG_027.png" alt="BGMのインスペクター情報" class="wp-image-1399" style="aspect-ratio:221/408" srcset="https://d9m.one/wp-content/uploads/2022/09/SideSTG_027.png 294w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_027-162x300.png 162w" sizes="(max-width: 294px) 100vw, 294px" /></a><figcaption class="wp-element-caption">BGMのインスペクター情報</figcaption></figure>
</div>
</div>



<p>・解説</p>



<p>オーディオ機能を使ってBGMを鳴らします。</p>



<p>・手順</p>



<p>BGMに合ったインポート設定に調整します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>プロジェクトの「Assets&gt;Space Shooter Assets&gt;Artwork&gt;music&gt;space-asteroids.ogg」をクリック</li>



<li>インスペクターの「バックグラウンドで実行」をチェック</li>



<li>インスペクターの「デフォルト＞ロードタイプ」を「ストリーミング」に変更</li>



<li>インスペクターの「適用する」をクリック</li>
</ol>



<p>ヒエラルキーに配置してBGM再生します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>プロジェクトの「Assets&gt;Space Shooter Assets&gt;Artwork&gt;music&gt;space-asteroids.ogg」をヒエラルキーにドラッグ＆ドロップ</li>



<li>F2キーを押して「BGM」と入力し、ENTERを押す</li>



<li>インスペクターの「Audio Source&gt;ループ」にチェックを入れる</li>
</ol>



<p>・動作確認</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー開始</li>



<li>BGMが再生されることを確認</li>



<li>BGMがループすることを確認</li>



<li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー終了</li>
</ol>



<h3 class="wp-block-heading"><span id="toc8">▷効果音再生制御</span></h3>



<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/09/SideSTG_029.png" target="_blank"><img decoding="async" width="294" height="499" src="https://d9m.one/wp-content/uploads/2022/09/SideSTG_029.png" alt="オーディオインポート設定" class="wp-image-1402" style="aspect-ratio:221/374" srcset="https://d9m.one/wp-content/uploads/2022/09/SideSTG_029.png 294w, https://d9m.one/wp-content/uploads/2022/09/SideSTG_029-177x300.png 177w" sizes="(max-width: 294px) 100vw, 294px" /></a><figcaption class="wp-element-caption">オーディオのインポート設定</figcaption></figure>
</div>



<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/09/SideSTG_030.png" target="_blank"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/09/SideSTG_030.png" alt="効果音ゲームオブジェクト３つのインスペクター情報" class="wp-image-1403" style="aspect-ratio:392/374"/></a><figcaption class="wp-element-caption">効果音ゲームオブジェクト３つのインスペクター情報</figcaption></figure>
</div>
</div>



<p>・解説</p>



<p>効果音を再生するためのスクリプトを作成します。<br>効果音再生にはオーディオ機能だけでなく、スクリプトも必要になります。</p>



<p>・手順</p>



<p>効果音を３つ、効果音に合ったインポート設定に調整します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>プロジェクトの「Assets&gt;Space Shooter Assets&gt;Artwork&gt;Sound FX&gt;explosion」をクリック</li>



<li>SHIFTを押しながらプロジェクトの「Assets&gt;Space Shooter Assets&gt;Artwork&gt;Sound FX&gt;shot 1」をクリック</li>



<li>インスペクターの「Mono に強制」をチェック</li>



<li>インスペクターの「適用する」をクリック</li>
</ol>



<p>ヒエラルキーに効果音を配置し、開始時に再生しないように設定します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>プロジェクトの「Assets&gt;Space Shooter Assets&gt;Artwork&gt;Sound FX&gt;explosion」をクリック</li>



<li>SHIFTを押しながらプロジェクトの「Assets&gt;Space Shooter Assets&gt;Artwork&gt;Sound FX&gt;shot 1」をクリック</li>



<li>プロジェクトで選択中のアセットをヒエラルキーの「Sample Scene」にドラッグ＆ドロップ</li>



<li>インスペクターの「Audio Source&gt;ゲーム開始時に再生」にあるチェックを外す</li>



<li>インスペクターの「Audio Source&gt;ボリューム」を(0.5)に設定</li>



<li>ヒエラルキーで選択中のゲームオブジェクトをヒエラルキーの「GameController」にドラッグ＆ドロップ</li>
</ol>



<p>ゲーム管理にオーディオソースを管理させます。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>プロジェクトの「Assets&gt;GameController」をダブルクリック</li>



<li>Start関数の前に以下の変数を追加</li>
</ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="GameController.cs" data-lang="C#"><code>    // オーディオソース一覧
    public List&lt;AudioSource&gt; audioSrcList = new List&lt;AudioSource&gt;();
</code></pre></div>



<p>ゲーム管理に効果音再生の機能を追加します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>playEffect関数の次に以下の関数を追加</li>
</ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="GameController.cs" data-lang="C#"><code>    // 効果音再生
    public void playSE(string clipName)
    {
        // オーディオクリップ一覧から指定されたと一致するオーディオクリップを取得
        AudioSource audioSrc = audioSrcList.Find(item =&gt; item.clip.name == clipName);
        // オーディオクリップが見つかった場合
        if (audioSrc != null)
        {
            // オーディオ再生
            audioSrc.Play();
        }
    }
</code></pre></div>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>CTRLを押しながらSキーを押してスクリプト保存</li>



<li>Unityエディタをクリックしてビルド</li>
</ol>



<p>命中した時に効果音を再生します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>プロジェクトの「Assets&gt;Hit」をダブルクリック</li>



<li>命中エフェクト再生の前に以下の処理を追加</li>
</ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Hit.cs" data-lang="C#"><code>            // 命中音再生
            gameController.playSE(&quot;hit&quot;);
</code></pre></div>



<p>爆発した時に効果音を再生します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>爆発エフェクト再生の前に以下の処理を追加</li>
</ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Hit.cs" data-lang="C#"><code>            // 爆発音再生
            gameController.playSE(&quot;explosion&quot;);
</code></pre></div>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>CTRLを押しながらSキーを押してスクリプト保存</li>



<li>Unityエディタをクリックしてビルド</li>
</ol>



<p>弾を発射した時に効果音を再生するため、Playerスクリプトを修正します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>プロジェクトの「Assets&gt;Player」をダブルクリック</li>



<li>Start関数の前に以下の処理を追加</li>
</ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Player.cs" data-lang="C#"><code>    GameController gameController;      // ゲーム管理オブジェクト
</code></pre></div>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>Start関数の最後の処理として以下を追加</li>
</ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Player.cs" data-lang="C#"><code>        // ゲーム管理オブジェクトを取得
        GameObject[] gameObjects = GameObject.FindGameObjectsWithTag(&quot;GameController&quot;);
        gameController = gameObjects[0].GetComponent&lt;GameController&gt;();
</code></pre></div>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>FixedUpdate関数内の「発射効果を表示」の前に以下の処理を追加</li>
</ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Player.cs" data-lang="C#"><code>            // 発射音再生
            gameController.playSE(&quot;shot 1&quot;);
</code></pre></div>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>CTRLを押しながらSキーを押してスクリプト保存</li>



<li>Unityエディタをクリックしてビルド</li>
</ol>



<p>プロジェクトを保存して、</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>CTRLを押しながらSキーを押してプロジェクト保存</li>
</ol>



<p>動作を確認します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー開始</li>



<li>弾発射のタイミングで発射音が再生されることを確認</li>



<li>弾が敵に命中した時に命中音が再生されることを確認</li>



<li>弾が爆発した時に爆発音が再生されることを確認</li>



<li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー終了</li>
</ol>



<h2 class="wp-block-heading"><span id="toc9">●完成</span></h2>



<p>敵にやられ、BGMと効果音も再生されるようになりました。<br>スコアは表示できませんでしたが、だいぶシューティングっぽくなったかな？</p>



<p>次回は「スコア表示、アステロイド作成」を実施しようと思います。</p>
<p>投稿 <a href="https://d9m.one/sidestg3/">オーディオ～横スクロールシューティング③</a> は <a href="https://d9m.one">だくまたゲーム制作ブログ</a> に最初に表示されました。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://d9m.one/sidestg3/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>敵作成～横スクロールシューティング②【2D】</title>
		<link>https://d9m.one/sidestg2/</link>
					<comments>https://d9m.one/sidestg2/#respond</comments>
		
		<dc:creator><![CDATA[だくまた]]></dc:creator>
		<pubDate>Sun, 21 Aug 2022 03:39:17 +0000</pubDate>
				<category><![CDATA[Unity]]></category>
		<category><![CDATA[チュートリアル]]></category>
		<guid isPermaLink="false">https://d9m.one/?p=1228</guid>

					<description><![CDATA[<p>目次 ●はじめに●敵作成▷敵アニメーション▷直進する敵▷上下にカーブしながら直進する敵●衝突処理作成▷衝突設定▷やられ制御●やられエフェクト▷やられアニメーション▷やられアニメーター▷やられエフェクト制御●完成 ●はじめ [&#8230;]</p>
<p>投稿 <a href="https://d9m.one/sidestg2/">敵作成～横スクロールシューティング②【2D】</a> は <a href="https://d9m.one">だくまたゲーム制作ブログ</a> に最初に表示されました。</p>
]]></description>
										<content:encoded><![CDATA[

  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-12" checked><label class="toc-title" for="toc-checkbox-12">目次</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">●はじめに</a></li><li><a href="#toc2" tabindex="0">●敵作成</a><ol><li><a href="#toc3" tabindex="0">▷敵アニメーション</a></li><li><a href="#toc4" tabindex="0">▷直進する敵</a></li><li><a href="#toc5" tabindex="0">▷上下にカーブしながら直進する敵</a></li></ol></li><li><a href="#toc6" tabindex="0">●衝突処理作成</a><ol><li><a href="#toc7" tabindex="0">▷衝突設定</a></li><li><a href="#toc8" tabindex="0">▷やられ制御</a></li></ol></li><li><a href="#toc9" tabindex="0">●やられエフェクト</a><ol><li><a href="#toc10" tabindex="0">▷やられアニメーション</a></li><li><a href="#toc11" tabindex="0">▷やられアニメーター</a></li><li><a href="#toc12" tabindex="0">▷やられエフェクト制御</a></li></ol></li><li><a href="#toc13" tabindex="0">●完成</a></li></ol>
    </div>
  </div>

<h2 class="wp-block-heading"><span id="toc1">●はじめに</span></h2>



<p>2D横スクロールシューティング作成の２回目になります。<br>アセットストアで無料公開されている「<a rel="noopener" href="https://assetstore.unity.com/packages/2d/characters/warped-space-shooter-181590#content" target="_blank">Warped Space Shooter</a>」をお借りして作成しています。</p>



<p>今回は「敵キャラクターを２パターン」を作成し、倒せるようになるまで進めます。</p>



<iframe loading="lazy" src="/game/sidestg2/index.html" width="780" height="465" scrolling="no" frameborder="0"></iframe>



<p>　↑このような形になります。</p>



<h2 class="wp-block-heading"><span id="toc2">●敵作成</span></h2>



<h3 class="wp-block-heading"><span id="toc3">▷敵アニメーション</span></h3>



<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/08/SideSTG_009.png" target="_blank"><img decoding="async" width="460" height="325" src="https://d9m.one/wp-content/uploads/2022/08/SideSTG_009.png" alt="アニメーションする敵を表示" class="wp-image-1234" srcset="https://d9m.one/wp-content/uploads/2022/08/SideSTG_009.png 460w, https://d9m.one/wp-content/uploads/2022/08/SideSTG_009-300x212.png 300w" sizes="(max-width: 460px) 100vw, 460px" /></a><figcaption class="wp-element-caption">アニメーションする敵を表示</figcaption></figure>



<p>・解説</p>



<p>アニメーション１枚目のスプライトをシーンに配置し、敵オブジェクトを作成します。<br>敵オブジェクトにコントローラーを割り当てて敵アニメーションと関連付けることで、アニメーションが再生されます。</p>



<p>・手順</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>「Assets&gt;Space Shooter Assets&gt;Artwork&gt;enemy&gt;enemy1.png」をシーンにドラッグ＆ドロップ</li>



<li>インスペクターの「Transform&gt;スケール」に(4, 4, 1)を入力</li>



<li>「Assets&gt;Space Shooter Assets&gt;Artwork&gt;enemy&gt;enemy1.controller」をヒエラルキーの「enemy1」にドラッグ＆ドロップ</li>
</ol>



<h3 class="wp-block-heading"><span id="toc4">▷直進する敵</span></h3>



<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/08/SideSTG_010.png" target="_blank"><img decoding="async" width="571" height="377" src="https://d9m.one/wp-content/uploads/2022/08/SideSTG_010.png" alt="敵がアニメーションしながら左に進む" class="wp-image-1242" srcset="https://d9m.one/wp-content/uploads/2022/08/SideSTG_010.png 571w, https://d9m.one/wp-content/uploads/2022/08/SideSTG_010-300x198.png 300w" sizes="(max-width: 571px) 100vw, 571px" /></a><figcaption class="wp-element-caption">敵がアニメーションしながら左に進む</figcaption></figure>



<p>・解説</p>



<p>自分の進行方向に向かって前進するだけのAIを作成します。<br>「１秒間に進む距離」を公開変数で持たせ、Unityで編集できるようにします。<br>物理更新処理で進行方向に経過時間を掛け、それを現在位置に足していくことで移動させます。</p>



<p>・手順</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>プロジェクトの「Assets」を右クリックし、「作成＞C# スクリプト」をクリック</li>



<li>Enemyと入力し、ENTERを押す</li>



<li>プロジェクトの「Assets&gt;Enemy」をヒエラルキーの「enemy1」にドラッグ＆ドロップ</li>



<li>プロジェクトの「Assets&gt;Enemy」をダブルクリック</li>



<li>Start関数の前に以下のスクリプトを記述</li>
</ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Enemy.cs" data-lang="C#"><code>    public float speed = 3;         // １秒間に進む距離
</code></pre></div>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>Update関数の後に以下のスクリプトを記述</li>
</ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Enemy.cs" data-lang="C#"><code>    // 物理更新時に処理
    void FixedUpdate()
    {
        // 現在の座標を取得
        Vector3 pos = transform.position;

        // 進行方向に向かって直進
        pos += -transform.right * speed * Time.fixedDeltaTime;

        // 新しい座標に変更
        transform.position = pos;
    }
</code></pre></div>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>CTRLを押しながらSキーを押してスクリプト保存</li>



<li>Unityエディタをクリックしてビルド</li>
</ol>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>CTRLを押しながらSキーを押してプロジェクト保存</li>
</ol>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー開始</li>



<li>敵機が左に向かって移動することを確認</li>



<li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー終了</li>
</ol>



<h3 class="wp-block-heading"><span id="toc5">▷上下にカーブしながら直進する敵</span></h3>



<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/08/SideSTG_011.png" target="_blank"><img decoding="async" width="571" height="377" src="https://d9m.one/wp-content/uploads/2022/08/SideSTG_011.png" alt="敵が上下にカーブしながら進む" class="wp-image-1243" srcset="https://d9m.one/wp-content/uploads/2022/08/SideSTG_011.png 571w, https://d9m.one/wp-content/uploads/2022/08/SideSTG_011-300x198.png 300w" sizes="(max-width: 571px) 100vw, 571px" /></a><figcaption class="wp-element-caption">敵が上下にカーブしながら進む</figcaption></figure>



<p>・解説</p>



<p>動きを表す変数「タイプ」を用意し、その値に応じて「直進のみ」「上下カーブあり」の動きを切り替えます。<br>タイプは公開変数としてUnity上で編集可能とします。<br>カーブは「サインカーブ」を使います。<br>物理更新された回数を記録しておき、その値をサインに渡します。<br>直進で得た移動量に掛け合わせることで、「上下にカーブしながら直進」する移動量となります。<br>これを現在位置に足すことで、「上下にカーブしながら直進」が実現できます。</p>



<p>・手順</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>プロジェクトの「Assets&gt;Enemy」をダブルクリック</li>



<li>変数「１秒間に進む距離」の前に以下のスクリプトを追加</li>
</ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Enemy.cs" data-lang="C#"><code>    // 動きの種類
    public enum ENEMY_TYPE {
        LINE,                       // まっすぐ進む
        CURVE                       // 上下にカーブ
    }

    // 動きの種類
    public ENEMY_TYPE type = ENEMY_TYPE.CURVE;
</code></pre></div>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>Start関数の前に以下のスクリプトを追加</li>
</ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Enemy.cs" data-lang="C#"><code>    public float cycleCount = 1;    // １秒間に往復する回数
    public float curveLength = 2;   // カーブの最大距離
    float cycleRadian = 0;          // サインに渡す値
    float centerY;                  // Y座標の中心
</code></pre></div>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>Start関数に以下のスクリプトを追加</li>
</ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Enemy.cs" data-lang="C#"><code>        // 初期Y座標を「Y座標の中心」として保存
        centerY = transform.position.y;
</code></pre></div>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>FixedUpdate関数内にある「新しい座標に変更」処理の前に以下のスクリプトを追加</li>
</ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Enemy.cs" data-lang="C#"><code>        // 上下にカーブ
        if (type == ENEMY_TYPE.CURVE)
        {
            if (cycleCount &gt; 0)
            {
                cycleRadian += (cycleCount * 2 * Mathf.PI) / 50;
                pos.y = Mathf.Sin(cycleRadian) * curveLength + centerY;
            }
        }
</code></pre></div>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>CTRLを押しながらSキーを押してスクリプト保存</li>



<li>Unityエディタをクリックしてビルド</li>
</ol>



<p>プロジェクトを保存して、</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>CTRLを押しながらSキーを押してプロジェクト保存</li>
</ol>



<p>動作を確認します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー開始</li>



<li>敵機が上下しながら左に向かって移動することを確認</li>



<li>１秒間に往復する回数「Cycle Count」を変えて動作確認</li>



<li>カーブの最大距離「Curve Length」を変えて動作確認</li>



<li>確認しずらかったら敵のSpeedを0にして確認</li>



<li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー終了</li>
</ol>



<h2 class="wp-block-heading"><span id="toc6">●衝突処理作成</span></h2>



<h3 class="wp-block-heading"><span id="toc7">▷衝突設定</span></h3>



<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/08/SideSTG_012.png" target="_blank"><img decoding="async" width="567" height="660" src="https://d9m.one/wp-content/uploads/2022/08/SideSTG_012.png" alt="インスペクター設定" class="wp-image-1264" srcset="https://d9m.one/wp-content/uploads/2022/08/SideSTG_012.png 567w, https://d9m.one/wp-content/uploads/2022/08/SideSTG_012-258x300.png 258w" sizes="(max-width: 567px) 100vw, 567px" /></a><figcaption class="wp-element-caption">インスペクター設定</figcaption></figure>



<p>・解説</p>



<p>衝突するようにするため、「Circle Collider 2D」と「Rigidbody 2D」を割り当てます。<br>敵機には両方、自機の弾には「Circle Collider 2D」のみを割り当てます。</p>



<p>2Dで作成しているので、「2D」と付いたコンポーネントを選ぶようにしてください。</p>



<p>・手順</p>



<p>敵機に「Circle Collider 2D」と「Rigidbody 2D」を割り当てます。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>ヒエラルキーの「enemy1」をクリック</li>



<li>インスペクターの「コンポーネントを追加」をクリック</li>



<li>「Physics 2D&gt;Circle Collider 2D」をクリック</li>



<li>インスペクターの「Circle Collider 2D&gt;トリガーにする」にチェックを入れる</li>



<li>インスペクターの「Circle Collider 2D&gt;半径」に(0.15)を入力</li>



<li>インスペクターの「コンポーネントを追加」をクリック</li>



<li>「Physics 2D&gt;Rigidbody 2D」をクリック</li>



<li>インスペクターの「Rigidbody 2D&gt;重力スケール」に(0)を入力</li>



<li>インスペクターの「Rigidbody 2D&gt;衝突判定」を(連続的)に変更</li>
</ol>



<p>自機の弾にコンポーネント「Circle Collider 2D」を割り当てます。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>プロジェクトの「Assets&gt;shoot1」をダブルクリック</li>



<li>インスペクターの「コンポーネントを追加」をクリック</li>



<li>「Physics 2D&gt;Circle Collider 2D」をクリック</li>



<li>インスペクターの「Circle Collider 2D&gt;トリガーにする」にチェックを入れる</li>



<li>インスペクターの「Circle Collider 2D&gt;オフセット&gt;X」に(0.05)を入力</li>



<li>インスペクターの「Circle Collider 2D&gt;半径」に(0.05)を入力</li>
</ol>



<p>弾が小さいままでした。。。スケールを４倍にします。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>インスペクターの「Transform&gt;スケール」に(4, 4, 1)を入力</li>



<li>ヒエラルキー左上の「＜」をクリック</li>
</ol>



<h3 class="wp-block-heading"><span id="toc8">▷やられ制御</span></h3>



<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/08/SideSTG_013.png" target="_blank"><img decoding="async" width="460" height="315" src="https://d9m.one/wp-content/uploads/2022/08/SideSTG_013.png" alt="弾を当てたら敵が消える" class="wp-image-1265" srcset="https://d9m.one/wp-content/uploads/2022/08/SideSTG_013.png 460w, https://d9m.one/wp-content/uploads/2022/08/SideSTG_013-300x205.png 300w, https://d9m.one/wp-content/uploads/2022/08/SideSTG_013-130x90.png 130w" sizes="(max-width: 460px) 100vw, 460px" /></a><figcaption class="wp-element-caption">弾を当てたら敵が消える</figcaption></figure>



<p>・解説</p>



<p>「体力が失くなったら爆発して消える」というスクリプトを作成し、自機と敵、それぞれに割り当てます。<br>コライダーを割り当てて衝突範囲を調整することで敵を倒せるようになります。</p>



<p>・手順</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>プロジェクトの「Assets」を右クリックし、「作成＞C# スクリプト」をクリック</li>



<li>Hitと入力し、ENTERを押す</li>



<li>プロジェクトの「Assets&gt;Hit」をヒエラルキーの「enemy1」にドラッグ＆ドロップ</li>



<li>プロジェクトの「Assets&gt;Hit」をダブルクリック</li>



<li>Start関数の前に以下のスクリプトを記述</li>
</ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Hit.cs" data-lang="C#"><code>    public int energy = 3;              // 残り体力
</code></pre></div>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>Update関数の後に以下のスクリプトを記述</li>
</ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Hit.cs" data-lang="C#"><code>    // 衝突時の処理（トリガー）
    void OnTriggerEnter2D(Collider2D collider)
    {
        // 体力を減らす
        energy--;
        // 体力が無くなった場合
        if (energy &lt;= 0)
        {
            // 自分を削除
            Destroy(gameObject);
        }
    }
</code></pre></div>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>CTRLを押しながらSキーを押してスクリプト保存</li>



<li>Unityエディタをクリックしてビルド</li>
</ol>



<p>プロジェクトを保存して、</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>CTRLを押しながらSキーを押してプロジェクト保存</li>
</ol>



<p>動作を確認します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー開始</li>



<li>弾を当てたら敵が消えることを確認</li>



<li>※体力を１にしても消えない場合は手順を見直してください</li>



<li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー終了</li>
</ol>



<h2 class="wp-block-heading"><span id="toc9">●やられエフェクト</span></h2>



<h3 class="wp-block-heading"><span id="toc10">▷やられアニメーション</span></h3>



<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/08/SideSTG_014.png" target="_blank"><img decoding="async" width="460" height="315" src="https://d9m.one/wp-content/uploads/2022/08/SideSTG_014.png" alt="命中した時（右下）と爆発した時（左下）のアニメーション、一時的にプレハブを置いての確認" class="wp-image-1266" srcset="https://d9m.one/wp-content/uploads/2022/08/SideSTG_014.png 460w, https://d9m.one/wp-content/uploads/2022/08/SideSTG_014-300x205.png 300w, https://d9m.one/wp-content/uploads/2022/08/SideSTG_014-130x90.png 130w" sizes="(max-width: 460px) 100vw, 460px" /></a><figcaption class="wp-element-caption">命中した時（右下）と爆発した時（左下）のアニメーション、一時的にプレハブを置いての確認</figcaption></figure>



<p>・解説</p>



<p>スッと消えてよくわからないのでエフェクトでリアクションしてもらいます。</p>



<p>エフェクトの１フレーム目のスプライトをシーンに配置してゲームオブジェクト化し、スケールを４倍にしてアニメーションを割り当て。<br>プレハブ化してヒエラルキーから削除、これを「Hit」と「explosion」のエフェクトに対して行います。</p>



<p>・手順</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>「Assets&gt;Space Shooter Assets&gt;Artwork&gt;hit&gt;hit1.png」をシーンにドラッグ＆ドロップ</li>



<li>インスペクターの「Transform&gt;スケール」に(4, 4, 1)を入力</li>



<li>「Assets&gt;Space Shooter Assets&gt;Artwork&gt;hit&gt;hit1.controller」をヒエラルキーの「hit1」にドラッグ＆ドロップ</li>



<li>ヒエラルキーの「hit1」をプロジェクトの「Assets」にドラッグ＆ドロップ</li>



<li>ヒエラルキーの「hit1」をクリックし、Deleteキーを押す</li>
</ol>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>「Assets&gt;Space Shooter Assets&gt;Artwork&gt;explosion&gt;explosion1.png」をシーンにドラッグ＆ドロップ</li>



<li>インスペクターの「Transform&gt;スケール」に(4, 4, 1)を入力</li>



<li>「Assets&gt;Space Shooter Assets&gt;Artwork&gt;explosion&gt;explosion1.controller」をヒエラルキーの「explosion1」にドラッグ＆ドロップ</li>



<li>ヒエラルキーの「explosion1」をプロジェクトの「Assets」にドラッグ＆ドロップ</li>



<li>ヒエラルキーの「explosion1」をクリックし、Deleteキーを押す</li>
</ol>



<h3 class="wp-block-heading"><span id="toc11">▷やられアニメーター</span></h3>



<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/08/SideSTG_015.png" target="_blank"><img decoding="async" width="460" height="315" src="https://d9m.one/wp-content/uploads/2022/08/SideSTG_015.png" alt="指示があった時に初めてアニメーション再生するよう修正" class="wp-image-1267" srcset="https://d9m.one/wp-content/uploads/2022/08/SideSTG_015.png 460w, https://d9m.one/wp-content/uploads/2022/08/SideSTG_015-300x205.png 300w, https://d9m.one/wp-content/uploads/2022/08/SideSTG_015-130x90.png 130w" sizes="(max-width: 460px) 100vw, 460px" /></a><figcaption class="wp-element-caption">指示があった時に初めてアニメーション再生するよう修正</figcaption></figure>



<figure class="wp-block-image size-full"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/08/SideSTG_017.png" target="_blank"><img decoding="async" width="742" height="256" src="https://d9m.one/wp-content/uploads/2022/08/SideSTG_017.png" alt="アニメーションに「画面上で見えなくするため」のフレームを追加" class="wp-image-1285" srcset="https://d9m.one/wp-content/uploads/2022/08/SideSTG_017.png 742w, https://d9m.one/wp-content/uploads/2022/08/SideSTG_017-300x104.png 300w" sizes="(max-width: 742px) 100vw, 742px" /></a><figcaption class="wp-element-caption">アニメーションに「画面上で見えなくするため」のフレームを追加</figcaption></figure>



<p>・解説</p>



<p>Hitとexplosionのフォルダにあるアニメーションを修正し、終了時に見えなくなるよう最終フレームのスケールを０にします。<br>また、アニメーション再生のタイミングを指定するためアニメーターコントローラーに空ステートを開始時のステートとして指定、<br>プロパティ「play」を追加してメインのアニメーションを呼び出すためのトリガーとします。</p>



<p>開始時は何も表示されず、スクリプトでplayトリガーを起動するとアニメーションが再生されるようになります。<br>この手順はそのための準備になります。</p>



<p>・手順</p>



<p>トリガー「play」がONになった時にはじめてアニメーションが再生されるように設定します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>「Assets&gt;Space Shooter Assets&gt;Artwork&gt;hit&gt;hit1.controller」をダブルクリック</li>



<li>アニメータービューの右側背景を右クリック</li>



<li>「ステートの作成＞空」をクリック</li>



<li>「New State」を右クリックし、「レイヤーデフォルトステートとして設定する」をクリック</li>



<li>「Any State」を右クリックして「遷移を作成」をクリック、そのまま「Hit-Animation」をクリックして矢印を繋げる</li>



<li>インスペクターのチェック「時間をループ」を外し、ループしないように設定</li>
</ol>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>アニメータービューのタブ「パラメーター」をクリック</li>



<li>アニメータービューの左側上部にある「＋」をクリックし、「Trigger」をクリック</li>



<li>「play」と入力してENTERを押す</li>



<li>「Any State」と「Hit-Animation」の間にある矢印をクリック</li>



<li>インスペクターの「Conditions」右下にある「＋」をクリック</li>
</ol>



<p>スケール０になるフレームを最終フレームに配置し、アニメーション終了時にスプライトが消えるように設定します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>プロジェクトの「Assets&gt;hit1」をダブルクリック</li>



<li>Unityメニューの「ウィンドウ＞アニメーション＞アニメーション」をクリック</li>



<li>アニメーションビューの「プロパティを追加」をクリック</li>



<li>「Transform」の左にある「▶」をクリック</li>



<li>「Transform&gt;スケール」の右にある「＋」をクリック</li>



<li>アニメーションビューのタイムライン部、上部にあるフレーム行の「３」をクリック</li>



<li>アニメーションビューのプロパティ部、右上にある「◆＋」をクリック</li>



<li>アニメーションビューのタイムライン部、上部にあるフレーム行の「４」をクリックし、４フレーム目に白い縦線を配置して操作対象とする</li>



<li>アニメーションビューのプロパティ部、右上にある「◆＋」をクリック</li>



<li>アニメーションビューのプロパティ部、スケールの左にある「▶」をクリック</li>



<li>アニメーションビューのタイムライン部、「スケール.x」の行にある「４フレーム目の◆」をクリック</li>



<li>アニメーションビューのプロパティ部、スケールのx, y, zに(0, 0, 0)を入力</li>
</ol>



<p>爆発エフェクトのアニメーターコントローラーを設定します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>「Assets&gt;Space Shooter Assets&gt;Artwork&gt;explosion&gt;explosion1.controller」をダブルクリック</li>



<li>アニメータービューの右側背景を右クリック</li>



<li>「ステートの作成＞空」をクリック</li>



<li>「New State」を右クリックし、「レイヤーデフォルトステートとして設定する」をクリック</li>



<li>「Any State」を右クリックして「遷移を作成」をクリック、そのまま「explosion-Animation」をクリックして矢印を繋げる</li>



<li>「explosion&#8211;Animation」をダブルクリック</li>



<li>インスペクターのチェック「時間をループ」を外し、ループしないように設定</li>
</ol>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>アニメータービューのタブ「パラメーター」をクリック</li>



<li>アニメータービューの左側上部にある「＋」をクリックし、「Trigger」をクリック</li>



<li>「play」と入力してENTERを押す</li>



<li>「Any State」と「explosion-animation」の間にある矢印をクリック</li>



<li>インスペクターの「Conditions」右下にある「＋」をクリック</li>
</ol>



<p>爆発エフェクトのアニメーションを設定します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>プロジェクトの「Assets&gt;explosion1」をダブルクリック</li>



<li>Unityメニューの「ウィンドウ＞アニメーション＞アニメーション」をクリック</li>



<li>アニメーションビューの「プロパティを追加」をクリック</li>



<li>「Transform」の左にある「▶」をクリック</li>



<li>「Transform&gt;スケール」の右にある「＋」をクリック</li>



<li>アニメーションビューのタイムライン部、上部にあるフレーム行の「４」をクリック</li>



<li>アニメーションビューのプロパティ部、右上にある「◆＋」をクリック</li>



<li>アニメーションビューのタイムライン部、上部にあるフレーム行の「５」をクリックし、５フレーム目に白い縦線を配置して操作対象とする</li>



<li>アニメーションビューのタイムライン部、「スケール.x」の行にある「５フレーム目の◆」をクリック</li>



<li>アニメーションビューのプロパティ部、右上にある「◆＋」をクリック</li>



<li>アニメーションビューのプロパティ部、スケールの左にある「▶」をクリック</li>



<li>アニメーションビューのプロパティ部、スケールのx, y, zに(0, 0, 0)を入力</li>



<li>ヒエラルキー左上の「&lt;」をクリックしてプレハブ編集を終了</li>
</ol>



<h3 class="wp-block-heading"><span id="toc12">▷やられエフェクト制御</span></h3>



<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/08/SideSTG_016.png" target="_blank"><img decoding="async" width="460" height="315" src="https://d9m.one/wp-content/uploads/2022/08/SideSTG_016.png" alt="命中した時、爆発した時にアニメーション再生" class="wp-image-1268" srcset="https://d9m.one/wp-content/uploads/2022/08/SideSTG_016.png 460w, https://d9m.one/wp-content/uploads/2022/08/SideSTG_016-300x205.png 300w, https://d9m.one/wp-content/uploads/2022/08/SideSTG_016-130x90.png 130w" sizes="(max-width: 460px) 100vw, 460px" /></a><figcaption class="wp-element-caption">命中した時、爆発した時にアニメーション再生</figcaption></figure>



<p>・解説</p>



<p>命中と爆発を管理するスクリプトを作成し、このスクリプトを呼び出すことでエフェクトが再生されるようにします。</p>



<p>・手順</p>



<p>ゲーム管理スクリプトを作成し、命中エフェクトを管理するための変数を定義します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>プロジェクトの「Assets」を右クリックし、「作成＞C# スクリプト」をクリック</li>



<li>GameControllerと入力し、ENTERを押す</li>



<li>ヒエラルキー左上の「＋」をクリック</li>



<li>「空のオブジェクトを作成」をクリック</li>



<li>「GameController」と入力してENTERを押す</li>



<li>プロジェクトの「Assets&gt;GameController」をヒエラルキーの「GameController」にドラッグ＆ドロップ</li>



<li>プロジェクトの「Assets&gt;GameController」をダブルクリック</li>



<li>Start関数の前に以下のスクリプトを記述</li>
</ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="GameController.cs" data-lang="C#"><code>    public GameObject hitEffectPrefab;              // 命中エフェクトプレハブ
    public int MAX_HIT_EFFECT = 100;                // 命中エフェクト最大数
    List&lt;GameObject&gt; hitEffects;                    // 命中エフェクト一覧
    int hitIndex = 0;                               // 次に使う命中エフェクトの位置
</code></pre></div>



<p>事前に命中エフェクトを作成して隠しておきます。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>Start関数に以下のスクリプトを記述</li>
</ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="GameController.cs" data-lang="C#"><code>        // カメラの後ろ位置
        Vector3 hidePos = Camera.main.transform.position - Camera.main.transform.forward;

        // 命中エフェクト事前作成
        hitEffects = new List&lt;GameObject&gt;();
        for (int i = 0; i &lt; MAX_HIT_EFFECT; i++)
        {
            hitEffects.Add(Instantiate(hitEffectPrefab));
            hitEffects[i].transform.position = hidePos;
        }
</code></pre></div>



<p>爆発エフェクトを管理するための変数を定義します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>Start関数の前に以下のスクリプトを記述</li>
</ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="GameController.cs" data-lang="C#"><code>    public GameObject explosionEffectPrefab;        // 爆発エフェクトプレハブ
    public int MAX_EXPLOSION_EFFECT = 100;          // 爆発エフェクト最大数
    List&lt;GameObject&gt; explosionEffects;              // 爆発エフェクト一覧
    int explosionIndex = 0;                         // 次に使う爆発エフェクトの位置
</code></pre></div>



<p>事前に爆発エフェクトを作成して隠しておきます。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>Start関数内の最後に以下のスクリプトを記述</li>
</ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="GameController.cs" data-lang="C#"><code>        // 爆発エフェクト事前作成
        explosionEffects = new List&lt;GameObject&gt;();
        for (int i = 0; i &lt; MAX_EXPLOSION_EFFECT; i++)
        {
            explosionEffects.Add(Instantiate(explosionEffectPrefab));
            explosionEffects[i].transform.position = hidePos;
        }
</code></pre></div>



<p>エフェクトを作成する処理を記述します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>Update関数の次に以下のスクリプトを記述</li>
</ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="GameController.cs" data-lang="C#"><code>    // エフェクト再生
    public void playEffect(string id, Vector3 pos)
    {
        GameObject effect = null;
        switch (id)
        {
            // 命中エフェクト
            case &quot;hit&quot;:
                effect = hitEffects[hitIndex];
                break;
            // 爆発エフェクト
            case &quot;explosion&quot;:
                effect = explosionEffects[explosionIndex];
                break;
        }

        if (effect != null)
        {
            // 位置設定
            effect.transform.position = pos;
            // アニメーション再生
            Animator animator = effect.GetComponent&lt;Animator&gt;();
            animator.SetTrigger(&quot;play&quot;);
        }
    }
</code></pre></div>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>CTRLを押しながらSキーを押してスクリプト保存</li>



<li>Unityエディタをクリックしてビルド</li>
</ol>



<p>Hitスクリプト側にエフェクトを呼び出す処理を記述します。<br>まずはGameControllerにアクセスするため、それを保持する変数を作成します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>プロジェクトの「Assets&gt;Hit」をダブルクリック</li>



<li>Start関数の前に以下のスクリプトを記述</li>
</ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Hit.cs" data-lang="C#"><code>    GameController gameController;      // ゲーム管理オブジェクト
</code></pre></div>



<p>タグを使ってGameControllerを取得します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>Start関数に以下のスクリプトを記述</li>
</ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Hit.cs" data-lang="C#"><code>        // ゲーム管理オブジェクトを取得
        GameObject[] gameObjects = GameObject.FindGameObjectsWithTag(&quot;GameController&quot;);
        gameController = gameObjects[0].GetComponent&lt;GameController&gt;();
</code></pre></div>



<p>体力が無くなったら自分の位置で爆発エフェクトを作成し、エフェクトを再生します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>「自分を削除」する処理の前に以下のスクリプトを記述</li>
</ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Hit.cs" data-lang="C#"><code>            // 爆発エフェクト再生
            gameController.playEffect(&quot;explosion&quot;, gameObject.transform.position);
</code></pre></div>



<p>体力が残っていたら自分の位置に命中エフェクトを作成し、エフェクトを再生します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>条件「体力が無くなった場合」の後に以下のスクリプトを記述</li>
</ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Hit.cs" data-lang="C#"><code>        // 体力が残っている場合
        else
        {
            // 命中エフェクト再生
            gameController.playEffect(&quot;hit&quot;, gameObject.transform.position);
        }
</code></pre></div>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>CTRLを押しながらSキーを押してスクリプト保存</li>



<li>Unityエディタをクリックしてビルド</li>
</ol>



<p>GameControllerを持ったゲームオブジェクトを検索できるようにタグを割り当てます。</p>



<p>最初から用意されている「GameController」を使います。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>ヒエラルキーの「GameController」をクリック</li>



<li>インスペクターの上部、左上にあるタグを「GameController」に変更</li>
</ol>



<p>命中エフェクト、爆発エフェクトとして扱うプレハブをGameControllerに割り当てます。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>プロジェクトの「Assets&gt;Hit1」をインスペクターの「Game Controller(スクリプト)&gt;Hit Effect Prefab」にドラッグ＆ドロップ</li>



<li>プロジェクトの「Assets&gt;explosion1」をインスペクターの「Game Controller(スクリプト)&gt;Explosion Effect Prefab」にドラッグ＆ドロップ</li>
</ol>



<p>プロジェクトを保存して、</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>CTRLを押しながらSキーを押してプロジェクト保存</li>
</ol>



<p>動作を確認します。</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list">
<li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー開始</li>



<li>弾を当てたら命中エフェクトが再生されることを確認</li>



<li>体力が無くなったら爆発エフェクトが再生されることを確認</li>



<li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー終了</li>
</ol>



<h2 class="wp-block-heading"><span id="toc13">●完成</span></h2>



<p>敵が上下移動しながら左に進み、弾を当てると爆発します。</p>



<p>シューティングの「敵作成」完了です。<br>敵を増やしたり、体力を増やしたり、種類を変えたりして試してみてください。</p>



<p>次回は「自機のやられ、BGM、効果音、スコア」といった辺りを進めようと思います。</p>
<p>投稿 <a href="https://d9m.one/sidestg2/">敵作成～横スクロールシューティング②【2D】</a> は <a href="https://d9m.one">だくまたゲーム制作ブログ</a> に最初に表示されました。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://d9m.one/sidestg2/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>プレイヤー作成～横スクロールシューティング①</title>
		<link>https://d9m.one/sidestg1/</link>
					<comments>https://d9m.one/sidestg1/#respond</comments>
		
		<dc:creator><![CDATA[だくまた]]></dc:creator>
		<pubDate>Fri, 12 Aug 2022 14:39:33 +0000</pubDate>
				<category><![CDATA[Unity]]></category>
		<category><![CDATA[チュートリアル]]></category>
		<guid isPermaLink="false">https://d9m.one/?p=1146</guid>

					<description><![CDATA[<p>目次 ●はじめに●準備▷プロジェクト作成▷素材探し▷素材取り込み●プレイヤー作成▷プレイヤー表示▷プレイヤー移動▷プレイヤー上昇下降●弾作成▷弾アニメーション▷弾の連続作成▷弾発射●完成 ●はじめに 2Dの横スクロールシ [&#8230;]</p>
<p>投稿 <a href="https://d9m.one/sidestg1/">プレイヤー作成～横スクロールシューティング①</a> は <a href="https://d9m.one">だくまたゲーム制作ブログ</a> に最初に表示されました。</p>
]]></description>
										<content:encoded><![CDATA[

  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-14" checked><label class="toc-title" for="toc-checkbox-14">目次</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">●はじめに</a></li><li><a href="#toc2" tabindex="0">●準備</a><ol><li><a href="#toc3" tabindex="0">▷プロジェクト作成</a></li><li><a href="#toc4" tabindex="0">▷素材探し</a></li><li><a href="#toc5" tabindex="0">▷素材取り込み</a></li></ol></li><li><a href="#toc6" tabindex="0">●プレイヤー作成</a><ol><li><a href="#toc7" tabindex="0">▷プレイヤー表示</a></li><li><a href="#toc8" tabindex="0">▷プレイヤー移動</a></li><li><a href="#toc9" tabindex="0">▷プレイヤー上昇下降</a></li></ol></li><li><a href="#toc10" tabindex="0">●弾作成</a><ol><li><a href="#toc11" tabindex="0">▷弾アニメーション</a></li><li><a href="#toc12" tabindex="0">▷弾の連続作成</a></li><li><a href="#toc13" tabindex="0">▷弾発射</a></li></ol></li><li><a href="#toc14" tabindex="0">●完成</a></li></ol>
    </div>
  </div>

<h2 class="wp-block-heading"><span id="toc1">●はじめに</span></h2>



<p>2Dの横スクロールシューティングを作ってみます。<br>アセットストアの無料アセットをお借りして背景、キャラクターを表示。<br>ゲームとしての制御はスクリプトを自作します。<br>ブラウザでもプレイできるよう弾は自動発射、自機はマウス位置／タップ位置を目指して動く形にします。</p>



<p>今回はプレイヤーキャラクターを作成して移動、弾発射まで進めます。</p>



<iframe loading="lazy" src="/game/sidestg1/index.html" width="780" height="465" scrolling="no" frameborder="0"></iframe>



<p>　↑このような形になります。</p>



<h2 class="wp-block-heading"><span id="toc2">●準備</span></h2>



<h3 class="wp-block-heading"><span id="toc3">▷プロジェクト作成</span></h3>



<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/08/SideSTG_000.png" target="_blank"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/08/SideSTG_000.png" alt="" class="wp-image-1186" width="720" height="405" srcset="https://d9m.one/wp-content/uploads/2022/08/SideSTG_000.png 960w, https://d9m.one/wp-content/uploads/2022/08/SideSTG_000-300x169.png 300w, https://d9m.one/wp-content/uploads/2022/08/SideSTG_000-768x432.png 768w" sizes="(max-width: 720px) 100vw, 720px" /></a><figcaption>2Dテンプレートでプロジェクト作成</figcaption></figure>



<p>・解説</p>



<p>２Dテンプレートを使ってプロジェクトを作成し、Unityゲーム開発を開始します。</p>



<p>・手順</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>UnityHUB起動</li><li>タブ「プロジェクト」をクリック</li><li>ボタン「新しいプロジェクト」をクリック</li><li>テンプレート「2D」をクリック</li><li>プロジェクト設定「プロジェクト名」を入力</li><li>プロジェクト設定「保存場所」を決定</li><li>ボタン「プロジェクトを作成」をクリック</li></ol>



<h3 class="wp-block-heading"><span id="toc4">▷素材探し</span></h3>



<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/08/SideSTG_001.png" target="_blank"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/08/SideSTG_001.png" alt="" class="wp-image-1187" width="736" height="426"/></a><figcaption>マイアセットに登録してUnityで開く</figcaption></figure>



<p>・解説</p>



<p>「2D」「無料」でフィルタリングしたところ９００件弱のアセットが見つかりました。<br>見落とすのも嫌なので「サムネイル」を目視して絞り込みます。<br>それっぽいアセットの概要を開き、概要のサムネイルで希望のものかチェック。</p>



<p>背景、機体、弾などバラバラで集めることになるかと思いましたが、一式揃ったアセットがありました。</p>



<p>「<a rel="noopener" href="https://assetstore.unity.com/packages/2d/characters/warped-space-shooter-181590#content" target="_blank">Warped Space Shooter</a>」</p>



<p>勉強やプロトタイプする時にはとても使いやすそうです。<br>作者さんの他アセットを確認したらどれも好きなレトロ感でした。<br>お借りします。</p>



<p>・手順</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>公式サイト「<a rel="noopener" href="https://assetstore.unity.com/" target="_blank">アセットストア</a>」にアクセス</li><li>フィルタ「無料」をチェック</li><li>横スクロールシューティングっぽいタイトル／サムネイルを探す</li><li>サムネイルにカーソルを合わせてアイコン「概要を見る」をクリック</li><li>サムネイルが作りたいものに合っているか確認</li><li>ボタン「完全な詳細を見る」をクリック</li><li>タブ「概要」の文章を見て作りたいものに合っているか確認</li><li>タブ「パッケージの内容」をクリック</li><li>フォルダを開いてファイルをクリック</li><li>プレビューに表示された画像が作りたいものに合っているか確認</li><li>使ってみる場合はボタン「マイアセットに登録」をクリック</li><li>ボタン「Unityで開く」をクリック</li></ol>



<h3 class="wp-block-heading"><span id="toc5">▷素材取り込み</span></h3>



<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/08/SideSTG_002-1.png" target="_blank"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/08/SideSTG_002-1.png" alt="" class="wp-image-1189" width="524" height="252" srcset="https://d9m.one/wp-content/uploads/2022/08/SideSTG_002-1.png 699w, https://d9m.one/wp-content/uploads/2022/08/SideSTG_002-1-300x144.png 300w" sizes="(max-width: 524px) 100vw, 524px" /></a><figcaption>インポート後のプロジェクト</figcaption></figure>



<p>・解説</p>



<p>Unityのプロジェクトにアセットを取り込みます。<br>アセットストアで「Unityで開く」をクリックして警告を許可すると、<br>Unityが前面に出てきて「パッケージマネージャー」というビューが表示されます。<br>ここでダウンロード、インポートを行ってUnityにアセットを取り込みます。</p>



<p>・手順</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>パッケージマネージャーのボタン「ダウンロード」をクリック</li><li>パッケージマネージャーのボタン「インポート」をクリック</li></ol>



<p>※アセットストアを経由せず、マイアセットから直接取り込む場合</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>Unityメニューの「ウィンドウ&gt;パッケージマネージャー」をクリック</li><li>パッケージマネージャーのドロップダウン「パッケージ」をクリックし、「マイアセット」をクリック</li><li>パッケージマネージャーの検索ボックスにキーワードを入力</li><li>パッケージマネージャーのアセット名をクリック</li><li>パッケージマネージャーのボタン「ダウンロード」をクリック</li><li>パッケージマネージャーのボタン「インポート」をクリック</li></ol>



<h2 class="wp-block-heading"><span id="toc6">●プレイヤー作成</span></h2>



<h3 class="wp-block-heading"><span id="toc7">▷プレイヤー表示</span></h3>



<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/08/SideSTG_003.png" target="_blank"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/08/SideSTG_003.png" alt="" class="wp-image-1190" width="720" height="405" srcset="https://d9m.one/wp-content/uploads/2022/08/SideSTG_003.png 960w, https://d9m.one/wp-content/uploads/2022/08/SideSTG_003-300x169.png 300w, https://d9m.one/wp-content/uploads/2022/08/SideSTG_003-768x432.png 768w" sizes="(max-width: 720px) 100vw, 720px" /></a><figcaption>自機の表示</figcaption></figure>



<p>・解説</p>



<p>「Assets&gt;Space Shooter Assets&gt;Artwork&gt;player」にあるplayer1をシーンにドラッグ＆ドロップし、位置を(-6, 0, 0)、スケールを(4, 4, 1)とします。</p>



<p>アニメーションは通常画像、上昇画像、下降画像を繰り返し表示するようになっています。<br>スクリプトもアニメーション設定も無いので、アセットストアの概要動画の動きはスクリプトで再現する必要がありそうです。</p>



<p>画像そのままのサイズでは小さすぎるので４倍にしています。<br>※インポート設定の「ユニット毎のピクセル数」で楽に制御できると思いますが、<br>　スプライトのみ表示して全選択するとインスペクターの表示がインポート設定じゃなくなってしまう。。。<br>　やり方あったら教えてほしいです。</p>



<p>・手順</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>プロジェクトの「Assets&gt;Space Shooter Assets&gt;Artwork&gt;player&gt;player1」をシーンにドラッグ＆ドロップ</li><li>インスペクターの「Transform&gt;位置」に(-6, 0, 0)を入力</li><li>インスペクターの「Transform&gt;スケール」に(4, 4, 1)を入力</li></ol>



<h3 class="wp-block-heading"><span id="toc8">▷プレイヤー移動</span></h3>



<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/08/SideSTG_004.png" target="_blank"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/08/SideSTG_004.png" alt="" class="wp-image-1191" width="720" height="405" srcset="https://d9m.one/wp-content/uploads/2022/08/SideSTG_004.png 960w, https://d9m.one/wp-content/uploads/2022/08/SideSTG_004-300x169.png 300w, https://d9m.one/wp-content/uploads/2022/08/SideSTG_004-768x432.png 768w" sizes="(max-width: 720px) 100vw, 720px" /></a><figcaption>マウスを追って自機が動く</figcaption></figure>



<p>・解説</p>



<p>マウスを追いかけるように自機を移動させるスクリプトを作成します。</p>



<p>・手順</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>プロジェクトの「Assets」を右クリックし、「作成＞C# スクリプト」をクリック</li><li>Playerと入力し、ENTERを押す</li><li>プロジェクトの「Assets&gt;Player」をヒエラルキーの「player1」にドラッグ＆ドロップ</li><li>プロジェクトの「Assets&gt;Player」をダブルクリック</li><li>Update関数の後に以下のスクリプトを記述</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Player.cs" data-lang="C#"><code>    // 物理更新時に処理
    void FixedUpdate()
    {
        // 移動量
        Vector3 moveVec;

        // プレイヤー移動
        {
            // マウス座標取得
            Vector3 pos = Input.mousePosition;
            // マウス座標をスクリーン座標に変換
            pos.z = -Camera.main.transform.position.z;
            // スクリーン座標をワールド座標に変換
            Vector3 worldPos = Camera.main.ScreenToWorldPoint(pos);
            // 現在位置からマウスワールド位置までの移動量
            moveVec = (worldPos - transform.position) / 30;
            // 徐々にマウス座標に近づくよう、移動量を分割して加算
            transform.position += moveVec;
        }
    }
</code></pre></div>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>CTRLを押しながらSキーを押してスクリプト保存</li><li>Unityエディタをクリックしてビルド</li></ol>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>CTRLを押しながらSキーを押してプロジェクト保存</li></ol>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー開始</li><li>マウスを追いかけるように自機が動くことを確認</li><li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー終了</li></ol>



<h3 class="wp-block-heading"><span id="toc9">▷プレイヤー上昇下降</span></h3>



<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/08/SideSTG_005.png" target="_blank"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/08/SideSTG_005.png" alt="" class="wp-image-1192" width="720" height="405" srcset="https://d9m.one/wp-content/uploads/2022/08/SideSTG_005.png 960w, https://d9m.one/wp-content/uploads/2022/08/SideSTG_005-300x169.png 300w, https://d9m.one/wp-content/uploads/2022/08/SideSTG_005-768x432.png 768w" sizes="(max-width: 720px) 100vw, 720px" /></a><figcaption>上下移動時にスプライトを切り替える</figcaption></figure>



<p>・解説</p>



<p>上下移動じにスプライトを切り替えることで、自機をアニメーションさせます。</p>



<p>Y移動量がマイナスの時は下降画像のplayer2、プラスの時は上昇画像のplayer3に切り替えます。<br>画像はUnityエディタ上で指定します。</p>



<p>・手順</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>プロジェクトの「Assets&gt;Player」をダブルクリック</li><li>Start関数の前に以下のスクリプトを記述</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Player.cs" data-lang="C#"><code>    public Sprite spriteNormal;         // プレイヤー通常スプライト
    public Sprite spriteDown;           // プレイヤー下降スプライト
    public Sprite spriteUp;             // プレイヤー上昇スプライト
</code></pre></div>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>FixedUpdate関数内の「プレイヤー移動」処理の後に以下のスクリプトを記述</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Player.cs" data-lang="C#"><code>        // 上下の移動量に応じてスプライトを切り替え
        SpriteRenderer spriteRenderer = GetComponent&lt;SpriteRenderer&gt;();
        if (Mathf.Abs(moveVec.y) &lt; 0.05f)
        {
            // 縦の移動量が0.05未満なら通常スプライト
            spriteRenderer.sprite = spriteNormal;
        }
        else
        {
            if (moveVec.y &lt; 0)
            {
                // 縦の移動量がマイナスなら下降スプライト
                spriteRenderer.sprite = spriteDown;
            }
            else
            {
                // 縦の移動量がプラスなら上昇スプライト
                spriteRenderer.sprite = spriteUp;
            }
        }
</code></pre></div>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>CTRLを押しながらSキーを押してスクリプト保存</li><li>Unityエディタをクリックしてビルド</li></ol>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>ヒエラルキーの「player1」をクリック</li><li>プロジェクトの「Assets&gt;Space Shooter Assets&gt;Artwork&gt;player&gt;player1」をインスペクターの「Player&gt;Sprite Normal」にドラッグ＆ドロップ</li><li>プロジェクトの「Assets&gt;Space Shooter Assets&gt;Artwork&gt;player&gt;player2」をインスペクターの「Player&gt;Sprite Down」にドラッグ＆ドロップ</li><li>プロジェクトの「Assets&gt;Space Shooter Assets&gt;Artwork&gt;player&gt;player3」をインスペクターの「Player&gt;Sprite Up」にドラッグ＆ドロップ</li><li>CTRLを押しながらSキーを押してプロジェクト保存</li></ol>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー開始</li><li>マウスを素早く上に動かすと自機の裏面が表示されることを確認</li><li>マウスに接近すると通常画像に戻ることを確認</li><li>マウスを素早く下に動かすと自機の表面が表示されることを確認</li><li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー終了</li></ol>



<h2 class="wp-block-heading"><span id="toc10">●弾作成</span></h2>



<h3 class="wp-block-heading"><span id="toc11">▷弾アニメーション</span></h3>



<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/08/SideSTG_006.png" target="_blank"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/08/SideSTG_006.png" alt="" class="wp-image-1193" width="524" height="252" srcset="https://d9m.one/wp-content/uploads/2022/08/SideSTG_006.png 699w, https://d9m.one/wp-content/uploads/2022/08/SideSTG_006-300x144.png 300w" sizes="(max-width: 524px) 100vw, 524px" /></a><figcaption>弾を量産するためにプレハブ化する</figcaption></figure>



<p>・解説</p>



<p>「Assets&gt;Space Shooter Assets&gt;Artwork&gt;shoot」にあるスプライト「shoot1」をシーンに配置し、<br>作られたゲームオブジェクトに「Assets&gt;Space Shooter Assets&gt;Artwork&gt;shoot」にあるアニメーションコントローラー「shoot1」を割り当てます。<br>ゲーム中にスクリプトで作成するため、プレハブ化してヒエラルキーから削除しておきます。</p>



<p>・手順</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>プロジェクトの「Assets&gt;Space Shooter Assets&gt;Artwork&gt;shoot&gt;shoot1.png」をシーンにドラッグ＆ドロップ</li><li>プロジェクトの「Assets&gt;Space Shooter Assets&gt;Artwork&gt;shoot&gt;shoot1.controller」をヒエラルキーの「shoot1」にドラッグ＆ドロップ</li><li>ヒエラルキーの「shoot1」をプロジェクトの「Assets」にドラッグ＆ドロップ</li><li>ヒエラルキーの「shoot1」をクリック</li><li>Deleteキーを押す</li></ol>



<h3 class="wp-block-heading"><span id="toc12">▷弾の連続作成</span></h3>



<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/08/SideSTG_007.png" target="_blank"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/08/SideSTG_007.png" alt="" class="wp-image-1194" width="360" height="245" srcset="https://d9m.one/wp-content/uploads/2022/08/SideSTG_007.png 480w, https://d9m.one/wp-content/uploads/2022/08/SideSTG_007-300x204.png 300w" sizes="(max-width: 360px) 100vw, 360px" /></a><figcaption>弾の連続作成</figcaption></figure>



<p>・解説</p>



<p>定期的に弾を発射するようにPlayerスクリプトを修正します。<br>オブジェクト作成や削除の処理は時間がかかるため、何度も繰り返すのは良くありません。<br>今回はプロトタイプレベルなので影響は出ないと思いますが、最悪処理落ちしてしまうためです。</p>



<p>事前に作成しておき、作成時に見える場所に持ってきて、削除時に見えない場所に持っていきます。<br>最大数は後で変えられるよう変数に持っておきましょう。</p>



<p>・手順</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>プロジェクトの「Assets&gt;Player」をダブルクリック</li><li>変数定義群の最後に以下の変数を追加</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Player.cs" data-lang="C#"><code>    public GameObject shotPrefab;       // 弾プレハブ
    public int maxShotCount = 20;       // 事前に弾を作成する数
    public float shotSpeed = 30;        // 弾が１秒間に進む距離
    List&lt;GameObject&gt; shotList;          // 弾キャッシュ（事前に作成する弾）
    int nextShotIndex = 0;              // 次に発射する弾キャッシュの位置
</code></pre></div>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>Start関数に以下の処理を記述</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Player.cs" data-lang="C#"><code>        // カメラの後ろ位置
        Vector3 hidePos = Camera.main.transform.position - Camera.main.transform.forward;

        // 弾を事前に作成し、カメラの後ろに配置
        shotList = new List&lt;GameObject&gt;();
        for (int i = 0; i &lt; maxShotCount; i++)
        {
            // プレハブから弾を作成
            shotList.Add(Instantiate(shotPrefab));
            shotList[i].transform.position = hidePos;
        }
</code></pre></div>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>FixedUpdate関数内の最後に処理を追加</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Player.cs" data-lang="C#"><code>        // 弾をプレイヤー位置に移動
        shotList[nextShotIndex].transform.position = transform.position;
        // 次の弾位置
        nextShotIndex++;
        if (nextShotIndex &gt;= maxShotCount)
        {
            nextShotIndex = 0;
        }
        // 弾移動（どうせ見えないので全部動かす）
        for (int i = 0; i &lt; maxShotCount; i++)
        {
            shotList[i].transform.position += transform.right * shotSpeed * Time.fixedDeltaTime;
        }
</code></pre></div>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>CTRLを押しながらSキーを押してスクリプト保存</li><li>Unityエディタをクリックしてビルド</li></ol>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>ヒエラルキーの「player1」をクリック</li><li>プロジェクトの「Assets&gt;shoot1」をインスペクターの「Player&gt;Shot Prefab」にドラッグ＆ドロップ</li><li>CTRLを押しながらSキーを押してプロジェクト保存</li></ol>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー開始</li><li>自機の先頭から弾が発射されることを確認</li><li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー終了</li></ol>



<h3 class="wp-block-heading"><span id="toc13">▷弾発射</span></h3>



<figure class="wp-block-image size-full is-resized"><a rel="noopener" href="https://d9m.one/wp-content/uploads/2022/08/SideSTG_008.png" target="_blank"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/08/SideSTG_008.png" alt="" class="wp-image-1195" width="345" height="244" srcset="https://d9m.one/wp-content/uploads/2022/08/SideSTG_008.png 460w, https://d9m.one/wp-content/uploads/2022/08/SideSTG_008-300x212.png 300w" sizes="(max-width: 345px) 100vw, 345px" /></a><figcaption>弾発射</figcaption></figure>



<p>・解説</p>



<p>弾は作られたけど、棒が自機の前に連なっているだけに見えます。<br>まず、弾作成の瞬間にエフェクトを表示して発射しているような効果を出しましょう。<br>さらに次の弾が発射されるまでの間隔を広げ。棒のように繋がらないよう修正します。</p>



<p>・手順</p>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>プロジェクトの「Assets&gt;Space Shooter Assets&gt;Artwork&gt;flash&gt;flash」をヒエラルキーの「player1」にドラッグ＆ドロップ</li><li>インスペクターの「Transform&gt;位置」に(0.185, 0, 0)を入力</li><li>インスペクターの「Transform&gt;スケール」に(1, 0.75, 1)を入力</li></ol>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>プロジェクトの「Assets&gt;Player」をダブルクリック</li><li>変数定義群の最後に以下の変数を追加</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Player.cs" data-lang="C#"><code>    public GameObject shotFlash;        // 発射効果ゲームオブジェクト
    const int FLASH_VIEW_FRAME = 4;     // 発射効果を継続して表示するフレーム数
    public int shotPerSec = 8;          // １秒間に発射する弾の数
    int shotFrame;                      // 前の弾を発射してからの経過フレーム数
</code></pre></div>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>FixedUpdate関数内の処理「弾をプレイヤー位置に移動」の前に以下の処理を追加</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Player.cs" data-lang="C#"><code>        // １秒間に発射する弾数に応じて「次の弾を発射するまでのフレーム数」を計算
        int shotInterval = 50 / shotPerSec;
</code></pre></div>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>FixedUpdate関数内の処理「弾をプレイヤー位置に移動」の前に以下の処理を追加</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Player.cs" data-lang="C#"><code>        // 弾発射
        shotFrame++;
        if (shotFrame &gt;= shotInterval)
        {
            // 次の弾待機を開始するため経過時間を初期化
            shotFrame = 0;

            // 発射効果を表示
            shotFlash.SetActive(true);
        }
        else if (shotFrame &gt;= FLASH_VIEW_FRAME)
        {
            // 発射効果の継続期間が過ぎたら発射効果を非表示
            shotFlash.SetActive(false);
        }
</code></pre></div>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>FixedUpdate関数内の処理「弾をプレイヤー位置に移動」を「発射効果を表示」の後に移動</li><li>FixedUpdate関数内の処理「次の弾位置」を「弾をプレイヤー位置に移動」の後に移動</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="Player.cs" data-lang="C#"><code>            // 弾をプレイヤー位置に移動
            shotList[nextShotIndex].transform.position = transform.position;
            // 次の弾位置
            nextShotIndex++;
            if (nextShotIndex &gt;= maxShotCount)
            {
                nextShotIndex = 0;
            }
</code></pre></div>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>CTRLを押しながらSキーを押してスクリプト保存</li><li>Unityエディタをクリックしてビルド</li></ol>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>ヒエラルキーの「player1」をクリック</li><li>ヒエラルキーの「player1&gt;flash」をインスペクターの「Player&gt;ShotFlash」にドラッグ＆ドロップ</li><li>CTRLを押しながらSキーを押してプロジェクト保存</li></ol>



<ol class="has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background wp-block-list"><li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー開始</li><li>自機の先頭にある発射効果が弾発射の瞬間だけ表示されることを確認</li><li>弾が１秒間に８発発射されることを確認</li><li>早すぎて納得できないならば、ShotPerSecを1にして確認</li><li>Unityエディタ上部中央のPlayボタンをクリックしてプレビュー終了</li></ol>



<h2 class="wp-block-heading"><span id="toc14">●完成</span></h2>



<p>弾は常に発射され、ゲームビュー内でカーソルを動かすとプレイヤーキャラクターが追いかけてきます。<br>ヒエラルキーを見ると20個の弾が常に表示されています。<br>新しく発射されても増えず、画面外に消えても減りません。<br>見た目も違和感ないので「プレイヤー作成」完了です。</p>
<p>投稿 <a href="https://d9m.one/sidestg1/">プレイヤー作成～横スクロールシューティング①</a> は <a href="https://d9m.one">だくまたゲーム制作ブログ</a> に最初に表示されました。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://d9m.one/sidestg1/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>【基本操作】タマあんどヒシガタ～３／３　ゲーム仕上げ【パズル】</title>
		<link>https://d9m.one/proc_tandh3/</link>
					<comments>https://d9m.one/proc_tandh3/#respond</comments>
		
		<dc:creator><![CDATA[だくまた]]></dc:creator>
		<pubDate>Fri, 22 Jul 2022 09:36:54 +0000</pubDate>
				<category><![CDATA[Unity]]></category>
		<category><![CDATA[基本操作]]></category>
		<category><![CDATA[自作ゲーム]]></category>
		<category><![CDATA[チュートリアル]]></category>
		<guid isPermaLink="false">https://d9m.one/?p=958</guid>

					<description><![CDATA[<p>●ステージ作成 ▷カメラ設定 ステージを見渡せるようカメラの姿勢を調整します。 ・位置、回転、スケール設定 ヒエラルキーの「Main Camera」をクリック インスペクターのTransformにある位置を(4.5, 1 [&#8230;]</p>
<p>投稿 <a href="https://d9m.one/proc_tandh3/">【基本操作】タマあんどヒシガタ～３／３　ゲーム仕上げ【パズル】</a> は <a href="https://d9m.one">だくまたゲーム制作ブログ</a> に最初に表示されました。</p>
]]></description>
										<content:encoded><![CDATA[
<h3 class="has-background has-medium-font-size wp-block-heading" style="background-color:#e5f7fd"><span id="toc1">●ステージ作成</span></h3>



<div style="height:16px" aria-hidden="true" class="wp-block-spacer"></div>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<h4 class="wp-block-heading"><span id="toc2">▷カメラ設定</span></h4>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>ステージを見渡せるようカメラの姿勢を調整します。</p>



<figure class="wp-block-image size-full is-resized"><a href="https://d9m.one/wp-content/uploads/2022/07/TandH_026.png"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/07/TandH_026.png" alt="" class="wp-image-846" width="658" height="475"/></a><figcaption>カメラ設定</figcaption></figure>



<p class="has-medium-font-size"><strong>・位置、回転、スケール設定</strong></p>



<ol class="wp-block-list"><li>ヒエラルキーの「Main Camera」をクリック</li><li>インスペクターのTransformにある位置を(4.5, 16, -2)とする</li><li>インスペクターのTransformにある回転を(65, 0, 0)とする</li><li>インスペクターのTransformにあるスケールを(1, 1, 1)とする</li></ol>



<p class="has-medium-font-size"><strong>・アスペクト比設定</strong></p>



<ol class="wp-block-list"><li>ゲームビュー上メニュー左から３番目ドロップダウンをクリック</li><li>「9:16 Aspect」をクリック、無い場合は以降を操作</li><li>左下の「＋」をクリック</li><li>ドロップダウン「タイプ」をクリックし、「アスペクト比」をクリック</li><li>幅＆高さの「X」に(9)、「Y」に(16)を入力</li><li>「OK」をクリック</li></ol>
</div>



<h4 class="wp-block-heading"><span id="toc3">▷プレハブ化</span></h4>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>ステージに２つ以上配置するオブジェクトをプレハブ化し、量産化しやすくします。<br>対象はトゲ、壁、ジャンプ台の３つです。</p>



<figure class="wp-block-image size-full is-resized"><a href="https://d9m.one/wp-content/uploads/2022/07/TandH_027.png"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/07/TandH_027.png" alt="" class="wp-image-848" width="658" height="475" srcset="https://d9m.one/wp-content/uploads/2022/07/TandH_027.png 877w, https://d9m.one/wp-content/uploads/2022/07/TandH_027-300x217.png 300w, https://d9m.one/wp-content/uploads/2022/07/TandH_027-768x554.png 768w" sizes="(max-width: 658px) 100vw, 658px" /></a><figcaption>プレハブ化</figcaption></figure>



<p class="has-medium-font-size"><strong>・プレハブ化</strong></p>



<ol class="wp-block-list"><li>ヒエラルキーの「Toge」をプロジェクトの「Assets」にドラッグ＆ドロップ</li><li>ヒエラルキーの「Block」をプロジェクトの「Assets」にドラッグ＆ドロップ</li><li>ヒエラルキーの「Jump」をプロジェクトの「Assets」にドラッグ＆ドロップ</li></ol>
</div>



<h4 class="wp-block-heading"><span id="toc4">▷単一オブジェクト配置</span></h4>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>タマ、ヒシガタ、ゴールの位置を設定します。</p>



<figure class="wp-block-image size-full is-resized"><a href="https://d9m.one/wp-content/uploads/2022/07/TandH_028.png"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/07/TandH_028.png" alt="" class="wp-image-849" width="658" height="475" srcset="https://d9m.one/wp-content/uploads/2022/07/TandH_028.png 877w, https://d9m.one/wp-content/uploads/2022/07/TandH_028-300x217.png 300w, https://d9m.one/wp-content/uploads/2022/07/TandH_028-768x554.png 768w" sizes="(max-width: 658px) 100vw, 658px" /></a><figcaption>単一オブジェクト配置</figcaption></figure>



<p class="has-medium-font-size"><strong>・位置設定</strong></p>



<ol class="wp-block-list"><li>ヒエラルキーの「Tama」をクリック</li><li>インスペクターのTransformにある位置を(1, 0, 8)とする</li><li>ヒエラルキーの「Hisi」をクリック</li><li>インスペクターのTransformにある位置を(8, 0, 1)とする</li><li>ヒエラルキーの「Goal」をクリック</li><li>インスペクターのTransformにある位置を(7, 0, 4)とする</li></ol>
</div>



<h4 class="wp-block-heading"><span id="toc5">▷トゲ配置</span></h4>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>トゲ配置用の空オブジェクトにトゲを格納し、複製しながら配置していきます。</p>



<figure class="wp-block-image size-full is-resized"><a href="https://d9m.one/wp-content/uploads/2022/07/TandH_029.png"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/07/TandH_029.png" alt="" class="wp-image-851" width="658" height="475" srcset="https://d9m.one/wp-content/uploads/2022/07/TandH_029.png 877w, https://d9m.one/wp-content/uploads/2022/07/TandH_029-300x217.png 300w, https://d9m.one/wp-content/uploads/2022/07/TandH_029-768x554.png 768w" sizes="(max-width: 658px) 100vw, 658px" /></a><figcaption>トゲ配置</figcaption></figure>



<p class="has-medium-font-size"><strong>・空オブジェクト作成</strong></p>



<ol class="wp-block-list"><li>ヒエラルキー左上の「＋」をクリック</li><li>「空のオブジェクトを作成」をクリック</li><li>「Toges」と入力し、ENTERキーを押す</li></ol>



<p class="has-medium-font-size"><strong>・位置スケールリセット</strong></p>



<ol class="wp-block-list"><li>インスペクターのTransformにある縦の三点リーダーをクリック</li><li>リセットをクリック</li></ol>



<p class="has-medium-font-size"><strong>・階層設定</strong></p>



<ol class="wp-block-list"><li>ヒエラルキーの「Toge」をヒエラルキーの「Toges」にドラッグ＆ドロップ</li></ol>



<p class="has-medium-font-size"><strong>・位置設定</strong></p>



<ol class="wp-block-list"><li>インスペクターのTransformにある位置を(1, 0, 0)とする</li></ol>



<p class="has-medium-font-size"><strong>・複製、位置設定</strong></p>



<ol class="wp-block-list"><li>ヒエラルキーの「Toges&gt;Toge」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターのTransformにある位置を(9, 0, 1)とする</li><li>ヒエラルキーの「Toges&gt;Toge (1)」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターのTransformにある位置を(8, 0, 9)とする</li><li>ヒエラルキーの「Toges&gt;Toge (2)」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターのTransformにある位置を(0, 0, 8)とする</li><li>ヒエラルキーの「Toges&gt;Toge (3)」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターのTransformにある位置を(5, 0, 4)とする</li><li>ヒエラルキーの「Toges&gt;Toge (4)」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターのTransformにある位置を(6, 0, 2)とする</li><li>ヒエラルキーの「Toges&gt;Toge (5)」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターのTransformにある位置を(5, 0, 2)とする</li><li>ヒエラルキーの「Toges&gt;Toge (6)」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターのTransformにある位置を(4, 0, 2)とする</li></ol>



<p class="has-medium-font-size"><strong>・階層を閉じる</strong></p>



<ol class="wp-block-list"><li>ヒエラルキーの「Toges」左にある「▼」をクリック</li></ol>
</div>



<h4 class="wp-block-heading"><span id="toc6">▷ジャンプ台配置</span></h4>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>ジャンプ台配置用の空オブジェクトにジャンプ台を格納し、複製しながら配置していきます。</p>



<figure class="wp-block-image size-full is-resized"><a href="https://d9m.one/wp-content/uploads/2022/07/TandH_030.png"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/07/TandH_030.png" alt="" class="wp-image-852" width="658" height="475" srcset="https://d9m.one/wp-content/uploads/2022/07/TandH_030.png 877w, https://d9m.one/wp-content/uploads/2022/07/TandH_030-300x217.png 300w, https://d9m.one/wp-content/uploads/2022/07/TandH_030-768x554.png 768w" sizes="(max-width: 658px) 100vw, 658px" /></a><figcaption>ジャンプ台配置</figcaption></figure>



<p class="has-medium-font-size"><strong>・空オブジェクト作成</strong></p>



<ol class="wp-block-list"><li>ヒエラルキー左上の「＋」をクリック</li><li>「空のオブジェクトを作成」をクリック</li><li>「Jumps」と入力し、ENTERキーを押す</li></ol>



<p class="has-medium-font-size"><strong>・位置スケールリセット</strong></p>



<ol class="wp-block-list"><li>インスペクターのTransformにある縦の三点リーダーをクリック</li><li>リセットをクリック</li></ol>



<p class="has-medium-font-size"><strong>・階層設定</strong></p>



<ol class="wp-block-list"><li>ヒエラルキーの「Jump」をヒエラルキーの「Jumps」にドラッグ＆ドロップ</li></ol>



<p class="has-medium-font-size"><strong>・位置設定</strong></p>



<ol class="wp-block-list"><li>インスペクターのTransformにある位置を(1, 0, 9)とする</li></ol>



<p class="has-medium-font-size"><strong>・複製、位置設定</strong></p>



<ol class="wp-block-list"><li>ヒエラルキーの「Jumps&gt;Jump」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターのTransformにある位置を(0, 0, 1)とする</li><li>ヒエラルキーの「Jumps&gt;Jump (1)」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターのTransformにある位置を(8, 0, 0)とする</li><li>ヒエラルキーの「Jumps&gt;Jump (2)」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターのTransformにある位置を(9, 0, 8)とする</li><li>ヒエラルキーの「Jumps&gt;Jump (3)」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターのTransformにある位置を(6, 0, 9)とする</li><li>ヒエラルキーの「Jumps&gt;Jump (4)」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターのTransformにある位置を(5, 0, 9)とする</li><li>ヒエラルキーの「Jumps&gt;Jump (5)」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターのTransformにある位置を(4, 0, 9)とする</li><li>ヒエラルキーの「Jumps&gt;Jump (6)」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターのTransformにある位置を(3, 0, 4)とする</li></ol>



<p class="has-medium-font-size"><strong>・階層を閉じる</strong></p>



<ol class="wp-block-list"><li>ヒエラルキーの「Jumps」左にある「▼」をクリック</li></ol>
</div>



<h4 class="wp-block-heading"><span id="toc7">▷壁配置</span></h4>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>壁配置用の空オブジェクトに壁を格納し、複製しながら配置していきます。</p>



<figure class="wp-block-image size-full is-resized"><a href="https://d9m.one/wp-content/uploads/2022/07/TandH_031.png"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/07/TandH_031.png" alt="" class="wp-image-854" width="658" height="475" srcset="https://d9m.one/wp-content/uploads/2022/07/TandH_031.png 877w, https://d9m.one/wp-content/uploads/2022/07/TandH_031-300x217.png 300w, https://d9m.one/wp-content/uploads/2022/07/TandH_031-768x554.png 768w" sizes="(max-width: 658px) 100vw, 658px" /></a><figcaption>壁配置</figcaption></figure>



<p class="has-medium-font-size"><strong>・空オブジェクト作成</strong></p>



<ol class="wp-block-list"><li>ヒエラルキー左上の「＋」をクリック</li><li>「空のオブジェクトを作成」をクリック</li><li>「Blocks」と入力し、ENTERキーを押す</li></ol>



<p class="has-medium-font-size"><strong>・位置スケールリセット</strong></p>



<ol class="wp-block-list"><li>インスペクターのTransformにある縦の三点リーダーをクリック</li><li>リセットをクリック</li></ol>



<p class="has-medium-font-size"><strong>・階層設定</strong></p>



<ol class="wp-block-list"><li>ヒエラルキーの「Block」をヒエラルキーの「Blocks」にドラッグ＆ドロップ</li></ol>



<p class="has-medium-font-size"><strong>・位置設定</strong></p>



<ol class="wp-block-list"><li>インスペクターのTransformにある位置を(0, 0, 9)とする</li></ol>



<p class="has-medium-font-size"><strong>・複製、位置設定</strong></p>



<ol class="wp-block-list"><li>ヒエラルキーの「Blocks&gt;Block」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターのTransformにある位置を(0, 0, 7)とする</li><li>ヒエラルキーの「Blocks&gt;Block (1)」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターのTransformにある位置を(0, 0, 6)とする</li><li>ヒエラルキーの「Blocks&gt;Block (2)」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターのTransformにある位置を(0, 0, 5)とする</li><li>ヒエラルキーの「Blocks&gt;Block (3)」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターのTransformにある位置を(0, 0, 4)とする</li><li>ヒエラルキーの「Blocks&gt;Block (4)」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターのTransformにある位置を(0, 0, 3)とする</li><li>ヒエラルキーの「Blocks&gt;Block (5)」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターのTransformにある位置を(0, 0, 2)とする</li><li>ヒエラルキーの「Blocks&gt;Block (6)」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターのTransformにある位置を(0, 0, 0)とする</li></ol>



<ol class="wp-block-list"><li>ヒエラルキーの「Blocks&gt;Block (7)」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターのTransformにある位置を(2, 0, 0)とする</li><li>ヒエラルキーの「Blocks&gt;Block (8)」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターのTransformにある位置を(3, 0, 0)とする</li><li>ヒエラルキーの「Blocks&gt;Block (9)」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターのTransformにある位置を(4, 0, 0)とする</li><li>ヒエラルキーの「Blocks&gt;Block (10)」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターのTransformにある位置を(5, 0, 0)とする</li><li>ヒエラルキーの「Blocks&gt;Block (11)」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターのTransformにある位置を(6, 0, 0)とする</li><li>ヒエラルキーの「Blocks&gt;Block (12)」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターのTransformにある位置を(7, 0, 0)とする</li><li>ヒエラルキーの「Blocks&gt;Block (13)」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターのTransformにある位置を(9, 0, 0)とする</li></ol>



<ol class="wp-block-list"><li>ヒエラルキーの「Blocks&gt;Block (14)」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターのTransformにある位置を(9, 0, 2)とする</li><li>ヒエラルキーの「Blocks&gt;Block (15)」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターのTransformにある位置を(9, 0, 3)とする</li><li>ヒエラルキーの「Blocks&gt;Block (16)」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターのTransformにある位置を(9, 0, 4)とする</li><li>ヒエラルキーの「Blocks&gt;Block (17)」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターのTransformにある位置を(9, 0, 5)とする</li><li>ヒエラルキーの「Blocks&gt;Block (18)」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターのTransformにある位置を(9, 0, 6)とする</li><li>ヒエラルキーの「Blocks&gt;Block (19)」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターのTransformにある位置を(9, 0, 7)とする</li><li>ヒエラルキーの「Blocks&gt;Block (20)」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターのTransformにある位置を(9, 0, 9)とする</li></ol>



<ol class="wp-block-list"><li>ヒエラルキーの「Blocks&gt;Block (21)」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターのTransformにある位置を(7, 0, 9)とする</li><li>ヒエラルキーの「Blocks&gt;Block (22)」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターのTransformにある位置を(3, 0, 9)とする</li><li>ヒエラルキーの「Blocks&gt;Block (23)」をクリック</li><li>CTRLを押しながらDキーを押す</li><li>インスペクターのTransformにある位置を(2, 0, 9)とする</li></ol>



<p class="has-medium-font-size"><strong>・階層を閉じる</strong></p>



<ol class="wp-block-list"><li>ヒエラルキーの「Blocks」左にある「▼」をクリック</li></ol>



<p class="has-medium-font-size"><strong>・プロジェクト保存</strong></p>



<ol class="wp-block-list"><li>CTRLを押しながらSキーを押す</li></ol>
</div>
</div>



<div style="height:16px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="has-background has-medium-font-size wp-block-heading" style="background-color:#e5f7fd"><span id="toc8">●ミスUI作成</span></h3>



<div style="height:16px" aria-hidden="true" class="wp-block-spacer"></div>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<h4 class="wp-block-heading"><span id="toc9">▷ミスUI作成</span></h4>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>タマがトゲ、もしくはヒシガタに衝突した時に「Miss!」というテキストを表示します。</p>



<figure class="wp-block-image size-full is-resized"><a href="https://d9m.one/wp-content/uploads/2022/07/TandH_032.png"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/07/TandH_032.png" alt="" class="wp-image-855" width="658" height="475" srcset="https://d9m.one/wp-content/uploads/2022/07/TandH_032.png 877w, https://d9m.one/wp-content/uploads/2022/07/TandH_032-300x217.png 300w, https://d9m.one/wp-content/uploads/2022/07/TandH_032-768x554.png 768w" sizes="(max-width: 658px) 100vw, 658px" /></a><figcaption>ミスUI作成（画像はオブジェクト無効化前）</figcaption></figure>



<p class="has-medium-font-size"><strong>・テキスト作成</strong></p>



<ol class="wp-block-list"><li>ヒエラルキー左上の「＋」をクリック</li><li>「UI＞テキスト &#8211; TextMeshPro」をクリック</li><li>「TextMiss」と入力し、ENTERキーを押す</li></ol>



<p class="has-medium-font-size"><strong>・TextMeshPro</strong>準備</p>



<ol class="wp-block-list"><li>TMP Importerの「Import TMP Essentials」をクリック</li><li>TMP Importerの右上にある「×」をクリック</li></ol>



<p class="has-medium-font-size"><strong>・UI位置設定</strong></p>



<ol class="wp-block-list"><li>インスペクターのRectTransform左上にあるアイコンをクリック</li><li>アンカープリセットの右下にある「Stretch &#8211; Stretch」をダブルクリック</li><li>インスペクターのRectTransform右上にある上下左右を全て「0」に設定</li></ol>



<p class="has-medium-font-size"><strong>・表示文字設定</strong></p>



<ol class="wp-block-list"><li>インスペクターのTextMeshProにある「Text Input&gt;テキスト入力」を「Miss!」に設定</li></ol>



<p class="has-medium-font-size"><strong>・文字装飾</strong></p>



<ol class="wp-block-list"><li>インスペクターのTextMeshProにある「Main Settings&gt;Font Style」にある「B」をクリック</li><li>インスペクターのTextMeshProにある「Main Settings&gt;Audo Size」のチェックボックスをクリック</li><li>インスペクターのTextMeshProにある「Main Settings&gt;Vertex Color」右のカラーバーをクリックし、色ダイアログの16進数を「FF0000」に設定</li><li>色ダイアログの右上にある「×」をクリック</li><li>インスペクターの「Shader&gt;Outline」左にあるチェックボックスをクリック</li><li>インスペクターの「Shader&gt;Outline」にある「Thickness」を「0.2」に設定</li></ol>



<p class="has-medium-font-size"><strong>・オブジェクト無効化</strong></p>



<ol class="wp-block-list"><li>インスペクター上部の左上にあるチェックボックスをクリック</li></ol>
</div>



<h4 class="wp-block-heading"><span id="toc10">▷ミス制御</span></h4>



<p>タマがトゲ、もしくはヒシガタとぶつかった時にミステキストを表示するようにCharCtrlを修正します。</p>



<figure class="wp-block-image size-full is-resized"><a href="https://d9m.one/wp-content/uploads/2022/07/TandH_033.png"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/07/TandH_033.png" alt="" class="wp-image-857" width="658" height="475" srcset="https://d9m.one/wp-content/uploads/2022/07/TandH_033.png 877w, https://d9m.one/wp-content/uploads/2022/07/TandH_033-300x217.png 300w, https://d9m.one/wp-content/uploads/2022/07/TandH_033-768x554.png 768w" sizes="(max-width: 658px) 100vw, 658px" /></a><figcaption>ミス制御</figcaption></figure>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p class="has-medium-font-size"><strong>・スクリプト編集開始</strong></p>



<ol class="wp-block-list"><li>プロジェクトの「Assets&gt;CharCtrl」をダブルクリック</li></ol>



<p class="has-medium-font-size"><strong>・スクリプト変数定義</strong></p>



<ol class="wp-block-list"><li>クラス定義内の先頭に以下の変数を追加</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="CharCtrl.cs" data-lang="C#"><code>    public GameObject TextMiss;         // ミスUI
</code></pre></div>



<p class="has-medium-font-size"><strong>・スクリプト関数修正</strong></p>



<ol class="wp-block-list"><li>「タマとヒシガタの位置が同じだった場合」の条件内にある「タマとヒシガタを削除」前に「ミスUIを有効化」する以下の処理を追加</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="CharCtrl.cs" data-lang="C#"><code>                // ミスUIを有効化
                TextMiss.SetActive(true);
</code></pre></div>



<p class="has-medium-font-size"><strong>・スクリプト関数修正</strong></p>



<ol class="wp-block-list"><li>「トゲと重なったら自分とトゲを削除」の条件内の先頭に「自分がタマだった場合、ミスUIを有効化」する処理を追加</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="CharCtrl.cs" data-lang="C#"><code>            // 自分がタマだった場合、ミスUIを有効化
            if (tag == &quot;Player&quot;)
            {
                TextMiss.SetActive(true);
            }
</code></pre></div>



<p class="has-medium-font-size"><strong>・スクリプト保存</strong></p>



<ol class="wp-block-list"><li>CTRLを押しながらSキーを押す</li></ol>



<p class="has-medium-font-size"><strong>・Unityに戻ってスクリプトをビルド</strong></p>



<ol class="wp-block-list"><li>Unityエディタのタイトルをクリック</li></ol>



<p class="has-medium-font-size"><strong>・オブジェクト割り当て</strong></p>



<ol class="wp-block-list"><li>ヒエラルキーの「Tama」をクリック</li><li>ヒエラルキーの「Canvas&gt;TextMiss」をインスペクターの「Char Ctrl (スクリプト)&gt;Text Miss」右の参照ボックスにドラッグ＆ドロップ</li></ol>



<p class="has-medium-font-size"><strong>・プロジェクト保存</strong></p>



<ol class="wp-block-list"><li>CTRLを押しながらSキーを押す</li></ol>
</div>



<h4 class="wp-block-heading"><span id="toc11">▷動作確認</span></h4>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>ミスした時にミステキストが表示されるかどうか、プレビューして確認します。</p>



<figure class="wp-block-image size-full is-resized"><a href="https://d9m.one/wp-content/uploads/2022/07/TandH_034.png"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/07/TandH_034.png" alt="" class="wp-image-859" width="658" height="475" srcset="https://d9m.one/wp-content/uploads/2022/07/TandH_034.png 877w, https://d9m.one/wp-content/uploads/2022/07/TandH_034-300x217.png 300w, https://d9m.one/wp-content/uploads/2022/07/TandH_034-768x554.png 768w" sizes="(max-width: 658px) 100vw, 658px" /></a><figcaption>動作確認</figcaption></figure>



<p class="has-medium-font-size"><strong>・プレビュー開始</strong></p>



<ol class="wp-block-list"><li>エディタ上中央の「▶」をクリック</li></ol>



<p class="has-medium-font-size"><strong>・動作確認</strong></p>



<ol class="wp-block-list"><li>ミスした時にミステキストが表示されるかどうか確認</li></ol>



<p class="has-medium-font-size"><strong>・プレビュー終了</strong></p>



<ol class="wp-block-list"><li>エディタ上中央の「▶」をクリック</li></ol>
</div>
</div>



<div style="height:16px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="has-background has-medium-font-size wp-block-heading" style="background-color:#e5f7fd"><span id="toc12">●クリアUI作成</span></h3>



<div style="height:16px" aria-hidden="true" class="wp-block-spacer"></div>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<h4 class="wp-block-heading"><span id="toc13">▷クリアUI作成</span></h4>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>タマがゴールに衝突した時に「Clear!」というテキストを表示します。</p>



<figure class="wp-block-image size-full is-resized"><a href="https://d9m.one/wp-content/uploads/2022/07/TandH_035.png"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/07/TandH_035.png" alt="" class="wp-image-861" width="658" height="475" srcset="https://d9m.one/wp-content/uploads/2022/07/TandH_035.png 877w, https://d9m.one/wp-content/uploads/2022/07/TandH_035-300x217.png 300w, https://d9m.one/wp-content/uploads/2022/07/TandH_035-768x554.png 768w" sizes="(max-width: 658px) 100vw, 658px" /></a><figcaption>クリアUI作成（画像はオブジェクト無効化前）</figcaption></figure>



<p class="has-medium-font-size"><strong>・オブジェクト複製</strong></p>



<ol class="wp-block-list"><li>ヒエラルキーの「Canvas&gt;TextMiss」をクリック</li><li>CTRLを押しながらDキーを押す</li></ol>



<p class="has-medium-font-size"><strong>・オブジェクト名変更</strong></p>



<ol class="wp-block-list"><li>F2キーを押して「TextClear」と入力し、ENTERを押す</li></ol>



<p class="has-medium-font-size"><strong>・オブジェクト有効化</strong></p>



<ol class="wp-block-list"><li>インスペクター上部の左上にあるチェックボックスをクリック</li></ol>



<p class="has-medium-font-size"><strong><strong>・表示文字設定</strong></strong></p>



<ol class="wp-block-list"><li>インスペクターのTextMeshProにある「Text Input&gt;テキスト入力」を「Clear!」に設定</li></ol>



<p class="has-medium-font-size"><strong><strong>・文字装飾</strong></strong></p>



<ol class="wp-block-list"><li>インスペクターのTextMeshProにある「Main Settings&gt;Vertex Color」右のカラーバーをクリックし、色ダイアログの16進数を「00FFFF」に設定</li><li>色ダイアログの右上にある「×」をクリック</li></ol>



<p class="has-medium-font-size"><strong><strong><strong>・オブジェクト無効化</strong></strong></strong></p>



<ol class="wp-block-list"><li>インスペクター上部の左上にあるチェックボックスをクリック</li></ol>
</div>



<h4 class="wp-block-heading"><span id="toc14">▷クリア制御</span></h4>



<p>タマがゴールと衝突した時にクリアテキストを表示するようにCharCtrlを修正します。</p>



<figure class="wp-block-image size-full is-resized"><a href="https://d9m.one/wp-content/uploads/2022/07/TandH_036.png"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/07/TandH_036.png" alt="" class="wp-image-862" width="658" height="475" srcset="https://d9m.one/wp-content/uploads/2022/07/TandH_036.png 877w, https://d9m.one/wp-content/uploads/2022/07/TandH_036-300x217.png 300w, https://d9m.one/wp-content/uploads/2022/07/TandH_036-768x554.png 768w" sizes="(max-width: 658px) 100vw, 658px" /></a><figcaption>クリア制御</figcaption></figure>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p class="has-medium-font-size"><strong><strong><strong>・スクリプト編集開始</strong></strong></strong></p>



<ol class="wp-block-list"><li>プロジェクトの「Assets&gt;CharCtrl」をダブルクリック</li></ol>



<p class="has-medium-font-size"><strong><strong><strong>・スクリプト変数定義</strong></strong></strong></p>



<ol class="wp-block-list"><li>ミスUIの定義の後に以下の変数を追加</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="CharCtrl.cs" data-lang="C#"><code>    public GameObject TextClear;        // クリアUI
</code></pre></div>



<p class="has-medium-font-size"><strong><strong><strong>・スクリプト関数修正</strong></strong></strong></p>



<ol class="wp-block-list"><li>「自分がタマで、かつゴールと重なった場合」の条件内処理の先頭に以下のスクリプトを追加</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="CharCtrl.cs" data-lang="C#"><code>            // クリアUIを有効化
            TextClear.SetActive(true);
</code></pre></div>



<p class="has-medium-font-size"><strong><strong><strong><strong>・スクリプト保存</strong></strong></strong></strong></p>



<ol class="wp-block-list"><li>CTRLを押しながらSキーを押す</li></ol>



<p class="has-medium-font-size"><strong><strong><strong><strong><strong><strong>・Unityに戻ってスクリプトをビルド</strong></strong></strong></strong></strong></strong></p>



<ol class="wp-block-list"><li>Unityエディタのタイトルをクリック</li></ol>



<p class="has-medium-font-size"><strong><strong><strong><strong><strong><strong>・オブジェクト割り当て</strong></strong></strong></strong></strong></strong></p>



<ol class="wp-block-list"><li>ヒエラルキーの「Tama」をクリック</li><li>ヒエラルキーの「Canvas&gt;TextClear」をインスペクターの「Char Ctrl (スクリプト)&gt;Text Clear」右の参照ボックスにドラッグ＆ドロップ</li></ol>



<p class="has-medium-font-size"><strong><strong><strong><strong><strong>・プロジェクト保存</strong></strong></strong></strong></strong></p>



<ol class="wp-block-list"><li>CTRLを押しながらSキーを押す</li></ol>
</div>



<h4 class="wp-block-heading"><span id="toc15">▷動作確認</span></h4>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>タマがゴールに衝突した時にクリアテキストが表示されるかどうか、プレビューして確認します。</p>



<figure class="wp-block-image size-full is-resized"><a href="https://d9m.one/wp-content/uploads/2022/07/TandH_037.png"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/07/TandH_037.png" alt="" class="wp-image-863" width="658" height="475" srcset="https://d9m.one/wp-content/uploads/2022/07/TandH_037.png 877w, https://d9m.one/wp-content/uploads/2022/07/TandH_037-300x217.png 300w, https://d9m.one/wp-content/uploads/2022/07/TandH_037-768x554.png 768w" sizes="(max-width: 658px) 100vw, 658px" /></a><figcaption>動作確認</figcaption></figure>



<p class="has-medium-font-size"><strong><strong><strong><strong><strong>・プレビュー開始</strong></strong></strong></strong></strong></p>



<ol class="wp-block-list"><li>エディタ上中央の「▶」をクリック</li></ol>



<p class="has-medium-font-size"><strong><strong><strong><strong><strong>・動作確認</strong></strong></strong></strong></strong></p>



<ol class="wp-block-list"><li>タマがゴールに衝突した時にクリアテキストが表示されるかどうか確認</li></ol>



<p class="has-medium-font-size"><strong>・プレビュー終了</strong></p>



<ol class="wp-block-list"><li>エディタ上中央の「▶」をクリック</li></ol>
</div>
</div>



<div style="height:16px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-text-align-center" style="font-size:80px"><strong>完成！</strong></p>



<div style="height:16px" aria-hidden="true" class="wp-block-spacer"></div>



<p>作成手順を書いた記事です。</p>



<p>　　　<a href="https://d9m.one/proc_tandh/">→紹介</a><br>　　　<a href="https://d9m.one/proc_tandh1/">→１／３　キャラクター作成</a><br>　　　<a href="https://d9m.one/proc_tandh2/" type="post">→２／３　オブジェクト作成</a><br>今▷　<a href="https://d9m.one/proc_tandh3/">→３／３　ゲーム仕上げ</a></p>



<p><a href="https://d9m.one/proc_tandh_full/">完全手順</a><br>※紹介と１～３の手順を１ページにまとめています。<br>　読み込みに時間がかかるかも知れません。</p>
<p>投稿 <a href="https://d9m.one/proc_tandh3/">【基本操作】タマあんどヒシガタ～３／３　ゲーム仕上げ【パズル】</a> は <a href="https://d9m.one">だくまたゲーム制作ブログ</a> に最初に表示されました。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://d9m.one/proc_tandh3/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>【基本操作】タマあんどヒシガタ～２／３　オブジェクト作成【パズル】</title>
		<link>https://d9m.one/proc_tandh2/</link>
					<comments>https://d9m.one/proc_tandh2/#respond</comments>
		
		<dc:creator><![CDATA[だくまた]]></dc:creator>
		<pubDate>Fri, 22 Jul 2022 09:35:28 +0000</pubDate>
				<category><![CDATA[Unity]]></category>
		<category><![CDATA[基本操作]]></category>
		<category><![CDATA[自作ゲーム]]></category>
		<category><![CDATA[チュートリアル]]></category>
		<guid isPermaLink="false">https://d9m.one/?p=954</guid>

					<description><![CDATA[<p>●床作成 タマやヒシガタが動き回る「地面」を作成します。 ▷床作成 平面を作成してTransformの位置を(4.5, -0.5, 4.5)とし、グレーのチェッカー模様に設定したマテリアルを割り当てます。 ・平面作成 ヒ [&#8230;]</p>
<p>投稿 <a href="https://d9m.one/proc_tandh2/">【基本操作】タマあんどヒシガタ～２／３　オブジェクト作成【パズル】</a> は <a href="https://d9m.one">だくまたゲーム制作ブログ</a> に最初に表示されました。</p>
]]></description>
										<content:encoded><![CDATA[
<div style="height:16px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="has-background has-medium-font-size wp-block-heading" style="background-color:#e5f7fd"><span id="toc1">●床作成</span></h3>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<div style="height:16px" aria-hidden="true" class="wp-block-spacer"></div>



<p>タマやヒシガタが動き回る「地面」を作成します。</p>



<h4 class="wp-block-heading"><span id="toc2">▷床作成</span></h4>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>平面を作成してTransformの位置を(4.5, -0.5, 4.5)とし、グレーのチェッカー模様に設定したマテリアルを割り当てます。</p>



<figure class="wp-block-image size-full is-resized"><a href="https://d9m.one/wp-content/uploads/2022/07/TandH_013.png"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/07/TandH_013.png" alt="" class="wp-image-823" width="658" height="475" srcset="https://d9m.one/wp-content/uploads/2022/07/TandH_013.png 877w, https://d9m.one/wp-content/uploads/2022/07/TandH_013-300x217.png 300w, https://d9m.one/wp-content/uploads/2022/07/TandH_013-768x554.png 768w" sizes="(max-width: 658px) 100vw, 658px" /></a><figcaption>床作成</figcaption></figure>



<p class="has-medium-font-size"><strong>・平面作成</strong></p>



<ol class="wp-block-list"><li>ヒエラルキー左上の「＋」をクリック</li><li>「3Dオブジェクト＞平面」をクリック</li><li>「Floor」と入力し、ENTERキーを押す</li></ol>



<p class="has-medium-font-size"><strong>・位置設定</strong></p>



<ol class="wp-block-list"><li>インスペクターのTransformにある位置を(4.5, -0.5, 4.5)とする</li></ol>



<p class="has-medium-font-size"><strong>・マテリアル作成</strong></p>



<ol class="wp-block-list"><li>プロジェクトの「Assets」を右クリックし、「作成＞マテリアル」をクリック</li><li>「matFloor」と入力し、ENTERキーを押す</li></ol>



<p class="has-medium-font-size"><strong>・マテリアルテクスチャ設定</strong></p>



<ol class="wp-block-list"><li>インスペクターの「Main Maps&gt;アルベド」左の◎をクリックし、「Default-Checker-Gray」をダブルクリック</li></ol>



<p class="has-medium-font-size"><strong>・マテリアル色設定</strong></p>



<ol class="wp-block-list"><li>インスペクターの「Main Maps&gt;アルベド＞カラーバー」をクリックし、色ダイアログの16進数を「CCCCCC」に設定</li><li>色ダイアログの右上にある「×」をクリック</li></ol>



<p class="has-medium-font-size"><strong>・マテリアルタイリング設定</strong></p>



<ol class="wp-block-list"><li>インスペクターの「Main Maps&gt;タイリング」を(5, 5)に設定</li></ol>



<p class="has-medium-font-size"><strong>・マテリアル割り当て</strong></p>



<ol class="wp-block-list"><li>プロジェクトの「matFloor」をヒエラルキーの「Floor」にドラッグ＆ドロップ</li></ol>



<p class="has-medium-font-size"><strong><strong><strong>・プロジェクト保存</strong></strong></strong></p>



<ol class="wp-block-list"><li>CTRLを押しながらSキーを押す</li></ol>
</div>



<div style="height:16px" aria-hidden="true" class="wp-block-spacer"></div>
</div>



<h3 class="has-background has-medium-font-size wp-block-heading" style="background-color:#e5f7fd"><span id="toc3">●壁作成</span></h3>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<div style="height:16px" aria-hidden="true" class="wp-block-spacer"></div>



<p>タマとヒシガタの移動を停止させる壁を作成します。<br>移動先に壁がある場合、推進力を失って停止します。</p>



<h4 class="wp-block-heading"><span id="toc4">▷壁作成</span></h4>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>箱を作成してTransformの位置を(0, 0, 5)とし、オレンジ色の「Background」に設定したマテリアルを割り当てます。</p>



<figure class="wp-block-image size-full is-resized"><a href="https://d9m.one/wp-content/uploads/2022/07/TandH_014.png"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/07/TandH_014.png" alt="" class="wp-image-825" width="658" height="475" srcset="https://d9m.one/wp-content/uploads/2022/07/TandH_014.png 877w, https://d9m.one/wp-content/uploads/2022/07/TandH_014-300x217.png 300w, https://d9m.one/wp-content/uploads/2022/07/TandH_014-768x554.png 768w" sizes="(max-width: 658px) 100vw, 658px" /></a><figcaption>壁作成</figcaption></figure>



<p class="has-medium-font-size"><strong>・箱作成</strong></p>



<ol class="wp-block-list"><li>ヒエラルキー左上の「＋」をクリック</li><li>「3Dオブジェクト＞キューブ」をクリック</li><li>「Block」と入力し、ENTERキーを押す</li></ol>



<p class="has-medium-font-size">・<strong>位置、回転、スケール設定</strong></p>



<ol class="wp-block-list"><li>インスペクターのTransformにある位置を(0, 0, 5)とする</li><li>インスペクターのTransformにある回転を(0, 0, 0)とする</li><li>インスペクターのTransformにあるスケールを(1, 1, 1)とする</li></ol>



<p class="has-medium-font-size"><strong>・マテリアル作成</strong></p>



<ol class="wp-block-list"><li>プロジェクトの「Assets」を右クリックし、「作成＞マテリアル」をクリック</li><li>「matBlock」と入力し、ENTERキーを押す</li></ol>



<p class="has-medium-font-size"><strong>・マテリアルテクスチャ設定</strong></p>



<ol class="wp-block-list"><li>インスペクターの「Main Maps&gt;アルベド」左の◎をクリックし、「Background」をダブルクリック</li></ol>



<p class="has-medium-font-size"><strong>・マテリアル色設定</strong></p>



<ol class="wp-block-list"><li>インスペクターの「Main Maps&gt;アルベド＞カラーバー」をクリックし、色ダイアログの16進数を「FF8000」に設定</li><li>色ダイアログの右上にある「×」をクリック</li></ol>



<p class="has-medium-font-size"><strong>・マテリアル割り当て</strong></p>



<ol class="wp-block-list"><li>プロジェクトの「matBlock」をヒエラルキーの「Block」にドラッグ＆ドロップ</li></ol>
</div>



<h4 class="wp-block-heading"><span id="toc5">▷壁認識</span></h4>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>タグ「Block」を作成し、壁に割り当てます。</p>



<figure class="wp-block-image size-full is-resized"><a href="https://d9m.one/wp-content/uploads/2022/07/TandH_015.png"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/07/TandH_015.png" alt="" class="wp-image-827" width="658" height="475" srcset="https://d9m.one/wp-content/uploads/2022/07/TandH_015.png 877w, https://d9m.one/wp-content/uploads/2022/07/TandH_015-300x217.png 300w, https://d9m.one/wp-content/uploads/2022/07/TandH_015-768x554.png 768w" sizes="(max-width: 658px) 100vw, 658px" /></a><figcaption>壁認識</figcaption></figure>



<p class="has-medium-font-size"><strong>・タグ作成</strong></p>



<ol class="wp-block-list"><li>ヒエラルキーの「Block」をクリック</li><li>インスペクター上部のタグドロップダウンをクリックし、「タグを追加&#8230;」をクリック</li><li>インスペクター「Tag &amp; Layers」にあるタグ欄右下の「＋」をクリック</li><li>「Block」と入力してENTERを押す</li></ol>



<p class="has-medium-font-size"><strong>・タグ割り当て</strong></p>



<ol class="wp-block-list"><li>ヒエラルキーの「Block」をクリック</li><li>インスペクターのタグドロップダウンをクリックし、「Block」をクリック</li></ol>
</div>



<h4 class="wp-block-heading"><span id="toc6">▷壁衝突</span></h4>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>移動先に壁かジャンプ台があったら移動停止するようにCharCtrlを修正します。</p>



<figure class="wp-block-image size-full is-resized"><a href="https://d9m.one/wp-content/uploads/2022/07/TandH_016.png"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/07/TandH_016.png" alt="" class="wp-image-828" width="658" height="475" srcset="https://d9m.one/wp-content/uploads/2022/07/TandH_016.png 877w, https://d9m.one/wp-content/uploads/2022/07/TandH_016-300x217.png 300w, https://d9m.one/wp-content/uploads/2022/07/TandH_016-768x554.png 768w" sizes="(max-width: 658px) 100vw, 658px" /></a><figcaption>壁衝突</figcaption></figure>



<p class="has-medium-font-size">・<strong>スクリプト編集開始</strong></p>



<ol class="wp-block-list"><li>プロジェクトの「Assets&gt;CharCtrl」をダブルクリック</li></ol>



<p class="has-medium-font-size"><strong>・スクリプト関数修正</strong></p>



<ol class="wp-block-list"><li>FixedUpdate関数内の「経過フレーム数を0初期化する」の後に以下のスクリプトを追加</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="CharCtrl.cs" data-lang="C#"><code>        // 移動先に壁かジャンプ台があったら移動終了
        GameObject moveObj = GetObject(transform.position + transform.forward);
        if (moveObj != null && (moveObj.tag == &quot;Block&quot; || moveObj.tag == &quot;Jump&quot;))
        {
            return;
        }
</code></pre></div>



<p class="has-medium-font-size"><strong>・スクリプト関数修正</strong></p>



<ol class="wp-block-list"><li>GetObject関数内の「処理対象とするタグの一覧を作成」を以下のスクリプトのように修正</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="CharCtrl.cs" data-lang="C#"><code>        // 処理対象とするタグの一覧を作成
        string[] tags = new string[] { &quot;Toge&quot;, &quot;Block&quot; };
</code></pre></div>



<p class="has-medium-font-size"><strong>・スクリプト保存</strong></p>



<ol class="wp-block-list"><li>CTRLを押しながらSキーを押す</li></ol>



<p class="has-medium-font-size"><strong>・Unityに戻ってスクリプトをビルド</strong></p>



<ol class="wp-block-list"><li>Unityエディタのタイトルをクリック</li></ol>



<p class="has-medium-font-size"><strong>・プロジェクト保存</strong></p>



<ol class="wp-block-list"><li>CTRLを押しながらSキーを押す</li></ol>
</div>



<h4 class="wp-block-heading"><span id="toc7">▷動作確認</span></h4>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>タマが壁に衝突したら止まるかどうか、プレビューして確認します。</p>



<figure class="wp-block-image size-full is-resized"><a href="https://d9m.one/wp-content/uploads/2022/07/TandH_017.png"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/07/TandH_017.png" alt="" class="wp-image-829" width="658" height="475" srcset="https://d9m.one/wp-content/uploads/2022/07/TandH_017.png 877w, https://d9m.one/wp-content/uploads/2022/07/TandH_017-300x217.png 300w, https://d9m.one/wp-content/uploads/2022/07/TandH_017-768x554.png 768w" sizes="(max-width: 658px) 100vw, 658px" /></a><figcaption>動作確認</figcaption></figure>



<p class="has-medium-font-size"><strong>・プレビュー開始</strong></p>



<ol class="wp-block-list"><li>エディタ上中央の「▶」をクリック</li></ol>



<p class="has-medium-font-size"><strong>・動作確認</strong></p>



<ol class="wp-block-list"><li>タマが壁に衝突したら止まることを確認</li></ol>



<p class="has-medium-font-size"><strong><strong>・プレビュー終了</strong></strong></p>



<ol class="has-medium-font-size wp-block-list"><li>エディタ上中央の「▶」をクリック</li></ol>
</div>



<div style="height:16px" aria-hidden="true" class="wp-block-spacer"></div>
</div>



<h3 class="has-background has-medium-font-size wp-block-heading" style="background-color:#e5f7fd"><span id="toc8">●ジャンプ台作成</span></h3>



<div style="height:16px" aria-hidden="true" class="wp-block-spacer"></div>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>画面をクリックするとジャンプ台の隣にあるタマとヒシガタを押すようにして移動を開始させます。<br>また、壁と同じように移動先にジャンプ台があると移動を停止します。</p>



<h4 class="wp-block-heading"><span id="toc9">▷ジャンプ台作成</span></h4>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>箱を作成してTransformの位置を(0, 0, -1)とし、緑色の「Knob」に設定したマテリアルを割り当てます。</p>



<figure class="wp-block-image size-full is-resized"><a href="https://d9m.one/wp-content/uploads/2022/07/TandH_018.png"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/07/TandH_018.png" alt="" class="wp-image-831" width="658" height="475" srcset="https://d9m.one/wp-content/uploads/2022/07/TandH_018.png 877w, https://d9m.one/wp-content/uploads/2022/07/TandH_018-300x217.png 300w, https://d9m.one/wp-content/uploads/2022/07/TandH_018-768x554.png 768w" sizes="(max-width: 658px) 100vw, 658px" /></a><figcaption>ジャンプ台作成</figcaption></figure>



<p class="has-medium-font-size"><strong>・箱作成</strong></p>



<ol class="wp-block-list"><li>ヒエラルキー左上の「＋」をクリック</li><li>「3Dオブジェクト＞キューブ」をクリック</li><li>「Jump」と入力し、ENTERキーを押す</li></ol>



<p class="has-medium-font-size"><strong>・<strong>位置、回転、スケール設定</strong></strong></p>



<ol class="wp-block-list"><li>インスペクターのTransformにある位置を(0, 0, -1)とする</li><li>インスペクターのTransformにある回転を(0, 0, 0)とする</li><li>インスペクターのTransformにあるスケールを(1, 1, 1)とする</li></ol>



<p class="has-medium-font-size"><strong><strong>・マテリアル作成</strong></strong></p>



<ol class="wp-block-list"><li>プロジェクトの「Assets」を右クリックし、「作成＞マテリアル」をクリック</li><li>「matJump」と入力し、ENTERキーを押す</li></ol>



<p class="has-medium-font-size"><strong><strong>・マテリアルテクスチャ設定</strong></strong></p>



<ol class="wp-block-list"><li>インスペクターの「Main Maps&gt;アルベド」左の◎をクリックし、「Knob」をダブルクリック</li></ol>



<p class="has-medium-font-size"><strong><strong>・マテリアル色設定</strong></strong></p>



<ol class="wp-block-list"><li>インスペクターの「Main Maps&gt;アルベド＞カラーバー」をクリックし、色ダイアログの16進数を「008000」に設定</li><li>色ダイアログの右上にある「×」をクリック</li></ol>



<p class="has-medium-font-size"><strong><strong>・マテリアル割り当て</strong></strong></p>



<ol class="wp-block-list"><li>プロジェクトの「matJump」をヒエラルキーの「Jump」にドラッグ＆ドロップ</li></ol>
</div>



<h4 class="wp-block-heading"><span id="toc10">▷ジャンプ台認識</span></h4>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>タグ「Jump」を作成し、ジャンプ台に割り当てます。</p>



<figure class="wp-block-image size-full is-resized"><a href="https://d9m.one/wp-content/uploads/2022/07/TandH_019.png"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/07/TandH_019.png" alt="" class="wp-image-833" width="658" height="475" srcset="https://d9m.one/wp-content/uploads/2022/07/TandH_019.png 877w, https://d9m.one/wp-content/uploads/2022/07/TandH_019-300x217.png 300w, https://d9m.one/wp-content/uploads/2022/07/TandH_019-768x554.png 768w" sizes="(max-width: 658px) 100vw, 658px" /></a><figcaption>ジャンプ台認識</figcaption></figure>



<p class="has-medium-font-size"><strong>・タグ作成</strong></p>



<ol class="wp-block-list"><li>ヒエラルキーの「Jump」をクリック</li><li>インスペクターのタグドロップダウンをクリックし、「タグを追加&#8230;」をクリック</li><li>インスペクター「Tag &amp; Layers」にあるタグ欄右下の「＋」をクリック</li><li>「Jump」と入力してENTERを押す</li></ol>



<p class="has-medium-font-size"><strong>・タグ割り当て</strong></p>



<ol class="wp-block-list"><li>ヒエラルキーの「Jump」をクリック</li><li>インスペクターのタグドロップダウンをクリックし、「Jump」をクリック</li></ol>
</div>



<h4 class="wp-block-heading"><span id="toc11">▷ジャンプ台制御</span></h4>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>ゲーム画面をクリックした時にタマかヒシガタが隣にあったら移動を開始するようCharCtrlを修正します。</p>



<figure class="wp-block-image size-full is-resized"><a href="https://d9m.one/wp-content/uploads/2022/07/TandH_020.png"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/07/TandH_020.png" alt="" class="wp-image-835" width="658" height="475" srcset="https://d9m.one/wp-content/uploads/2022/07/TandH_020.png 877w, https://d9m.one/wp-content/uploads/2022/07/TandH_020-300x217.png 300w, https://d9m.one/wp-content/uploads/2022/07/TandH_020-768x554.png 768w" sizes="(max-width: 658px) 100vw, 658px" /></a><figcaption>ジャンプ台制御</figcaption></figure>



<p class="has-medium-font-size"><strong>・スクリプト編集開始</strong></p>



<ol class="wp-block-list"><li>プロジェクトの「Assets&gt;CharCtrl」をダブルクリック</li></ol>



<p class="has-medium-font-size"><strong>・スクリプト変数追加</strong></p>



<ol class="wp-block-list"><li>「１歩に何フレームかけるか」の変数の前に以下の変数を追加</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>    bool moveFlag = false;      // 移動中かどうか
</code></pre></div>



<p class="has-medium-font-size"><strong>・スクリプト関数修正</strong></p>



<ol class="wp-block-list"><li>Update関数内に以下のスクリプトを記述</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="CharCtrl.cs" data-lang="C#"><code>        // マウスクリックされた場合
        if (Input.GetMouseButtonUp(0) == true)
        {
        }
</code></pre></div>



<p class="has-medium-font-size"><strong>・スクリプト関数修正</strong></p>



<ol class="wp-block-list"><li>Update関数内の「マウスクリックされた場合」の条件内に以下のスクリプトを記述</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="CharCtrl.cs" data-lang="C#"><code>            // 現在の回転値を保存
            Quaternion rot = transform.rotation;
            // 上下左右のオブジェクトを取得
            transform.rotation = Quaternion.Euler(0, 0, 0);
            GameObject objU = GetObject(transform.position + transform.forward);
            GameObject objD = GetObject(transform.position - transform.forward);
            GameObject objL = GetObject(transform.position - transform.right);
            GameObject objR = GetObject(transform.position + transform.right);
</code></pre></div>



<p class="has-medium-font-size"><strong>・スクリプト関数修正</strong></p>



<ol class="wp-block-list"><li>Update関数内の「上下左右のオブジェクトを取得」処理の後に以下のスクリプトを記述</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="CharCtrl.cs" data-lang="C#"><code>            // 指定方向がジャンプ台かつ逆側がなにも無いかトゲの場合、オブジェクトをジャンプ台の逆側に向ける
            float dir = -1;
            if (objU != null && objU.tag == &quot;Jump&quot; && (objD == null || objD.tag == &quot;Toge&quot;)) dir = 180;
            if (objD != null && objD.tag == &quot;Jump&quot; && (objU == null || objU.tag == &quot;Toge&quot;)) dir = 0;
            if (objL != null && objL.tag == &quot;Jump&quot; && (objR == null || objR.tag == &quot;Toge&quot;)) dir = 90;
            if (objR != null && objR.tag == &quot;Jump&quot; && (objL == null || objL.tag == &quot;Toge&quot;)) dir = 270;
            if (dir != -1)
            {
                moveFlag = true;
                rot = Quaternion.Euler(0, dir, 0);
            }
            // 回転値を設定
            transform.rotation = rot;
</code></pre></div>



<p class="has-medium-font-size"><strong>・スクリプト関数修正</strong></p>



<ol class="wp-block-list"><li>FixedUpdate関数内の「経過フレーム数を0初期化する」処理の後に以下のスクリプトを記述</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="CharCtrl.cs" data-lang="C#"><code>        // 移動していない場合は終了
        if (moveFlag == false) return;
</code></pre></div>



<p class="has-medium-font-size"><strong>・スクリプト関数修正</strong></p>



<ol class="wp-block-list"><li>FixedUpdate関数内の「移動先に壁かジャンプ台があったら移動終了」条件内のreturn前に以下のスクリプトを記述</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="CharCtrl.cs" data-lang="C#"><code>            moveFlag = false;
</code></pre></div>



<p class="has-medium-font-size"><strong>・スクリプト関数修正</strong></p>



<ol class="wp-block-list"><li>GetObject関数内の「処理対象とするタグの一覧を作成」を以下のスクリプトのように修正</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="CharCtrl.cs" data-lang="C#"><code>        // 処理対象とするタグの一覧を作成
        string[] tags = new string[] { &quot;Toge&quot;, &quot;Block&quot;, &quot;Jump&quot; };
</code></pre></div>



<p class="has-medium-font-size"><strong>・スクリプト保存</strong></p>



<ol class="wp-block-list"><li>CTRLを押しながらSキーを押す</li></ol>



<p class="has-medium-font-size"><strong><strong>・Unityに戻ってスクリプトをビルド</strong></strong></p>



<ol class="wp-block-list"><li>Unityエディタのタイトルをクリック</li></ol>



<p class="has-medium-font-size"><strong>・プロジェクト保存</strong></p>



<ol class="wp-block-list"><li>CTRLを押しながらSキーを押す</li></ol>
</div>



<h4 class="wp-block-heading"><span id="toc12">▷動作確認</span></h4>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>クリックしたらタマが動き出すかどうか、プレビューして確認します。</p>



<figure class="wp-block-image size-full is-resized"><a href="https://d9m.one/wp-content/uploads/2022/07/TandH_021.png"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/07/TandH_021.png" alt="" class="wp-image-837" width="658" height="475" srcset="https://d9m.one/wp-content/uploads/2022/07/TandH_021.png 877w, https://d9m.one/wp-content/uploads/2022/07/TandH_021-300x217.png 300w, https://d9m.one/wp-content/uploads/2022/07/TandH_021-768x554.png 768w" sizes="(max-width: 658px) 100vw, 658px" /></a><figcaption>動作確認</figcaption></figure>



<p class="has-medium-font-size"><strong>・プレビュー開始</strong></p>



<ol class="wp-block-list"><li>エディタ上中央の「▶」をクリック</li></ol>



<p class="has-medium-font-size"><strong>・動作確認</strong></p>



<ol class="wp-block-list"><li>タマとヒシガタが動いていないことを確認</li><li>ゲームビューをクリックしたらジャンプ台と隣接しているタマが動き出すことを確認</li></ol>



<p class="has-medium-font-size"><strong>・プレビュー終了</strong></p>



<ol class="wp-block-list"><li>エディタ上中央の「▶」をクリック</li></ol>
</div>



<h4 class="wp-block-heading"><span id="toc13">▷スクリプト記述例</span></h4>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>「クリックしたらタマが動き出す」スクリプトの記述例です。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="CharCtrl" data-lang="C#"><code>using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharCtrl : MonoBehaviour
{
    bool moveFlag = false;      // 移動中かどうか
    float moveFrame = 30;       // １歩に何フレームかけるか
    float frameCount = 0;       // 経過フレーム数

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        // マウスクリックされた場合
        if (Input.GetMouseButtonUp(0) == true)
        {
            // 現在の回転値を保存
            Quaternion rot = transform.rotation;
            // 上下左右のオブジェクトを取得
            transform.rotation = Quaternion.Euler(0, 0, 0);
            GameObject objU = GetObject(transform.position + transform.forward);
            GameObject objD = GetObject(transform.position - transform.forward);
            GameObject objL = GetObject(transform.position - transform.right);
            GameObject objR = GetObject(transform.position + transform.right);
            // 指定方向がジャンプ台かつ逆側がなにも無いかトゲの場合、オブジェクトをジャンプ台の逆側に向ける
            float dir = -1;
            if (objU != null && objU.tag == &quot;Jump&quot; && (objD == null || objD.tag == &quot;Toge&quot;)) dir = 180;
            if (objD != null && objD.tag == &quot;Jump&quot; && (objU == null || objU.tag == &quot;Toge&quot;)) dir = 0;
            if (objL != null && objL.tag == &quot;Jump&quot; && (objR == null || objR.tag == &quot;Toge&quot;)) dir = 90;
            if (objR != null && objR.tag == &quot;Jump&quot; && (objL == null || objL.tag == &quot;Toge&quot;)) dir = 270;
            if (dir != -1)
            {
                moveFlag = true;
                rot = Quaternion.Euler(0, dir, 0);
            }
            // 回転値を設定
            transform.rotation = rot;
        }
    }

    // 物理フレーム更新時に処理
    void FixedUpdate()
    {
        // タグ検索でタマとヒシガタの一覧を取得する
        GameObject[] tamas = GameObject.FindGameObjectsWithTag(&quot;Player&quot;);
        GameObject[] hisis = GameObject.FindGameObjectsWithTag(&quot;Enemy&quot;);

        // １歩分のフレームが経過するまで何もしない
        frameCount++;
        if (frameCount &lt; moveFrame) return;

        // 経過フレーム数を0初期化する
        frameCount = 0;

        // 移動していない場合は終了
        if (moveFlag == false) return;

        // 移動先に壁かジャンプ台があったら移動終了
        GameObject moveObj = GetObject(transform.position + transform.forward);
        if (moveObj != null && (moveObj.tag == &quot;Block&quot; || moveObj.tag == &quot;Jump&quot;))
        {
            moveFlag = false;
            return;
        }

        // 向いている方向に１メートル進む
        transform.position += transform.forward;

        // ヒシガタとタマが衝突したら両方削除
        if (tamas.Length != 0 && hisis.Length != 0)
        {
            // タマとヒシガタの位置が同じだった場合
            if (tamas[0].transform.position == hisis[0].transform.position)
            {
                // タマとヒシガタを削除
                Destroy(tamas[0]);
                Destroy(hisis[0]);
            }
        }

        // 重なったオブジェクトを取得
        GameObject obj = GetObject(transform.position);
        // トゲと重なったら自分とトゲを削除
        if (obj != null && obj.tag == &quot;Toge&quot;)
        {
            Destroy(gameObject);
            Destroy(obj);
        }
    }

    // 指定位置にあるオブジェクトを取得
    GameObject GetObject(Vector3 pos)
    {
        // 小数点誤差を補正
        float posX = Mathf.Floor(pos.x + 0.5f);
        float posZ = Mathf.Floor(pos.z + 0.5f);
        // 処理対象とするタグの一覧を作成
        string[] tags = new string[] { &quot;Toge&quot;, &quot;Block&quot;, &quot;Jump&quot; };
        // タグを１つ１つ処理
        for (int i = 0; i &lt; tags.Length; i++)
        {
            // 指定タグのオブジェクト一覧を取得
            GameObject[] objs = GameObject.FindGameObjectsWithTag(tags[i]);
            // オブジェクトを１つ１つ処理
            for (int j = 0; j &lt; objs.Length; j++)
            {
                // 指定位置に存在するオブジェクトを返す
                Vector3 objPos = objs[j].transform.position;
                if (objPos.x == posX && objPos.z == posZ) return objs[j];
            }
        }

        // 指定位置にオブジェクトが無いのでNULLを返す
        return null;
    }
}</code></pre></div>
</div>
</div>



<div style="height:16px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="has-background has-medium-font-size wp-block-heading" style="background-color:#e5f7fd"><span id="toc14">●ゴール作成</span></h3>



<div style="height:16px" aria-hidden="true" class="wp-block-spacer"></div>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>タマと衝突するとクリアになる、「ゴール」を作成します。</p>



<h4 class="wp-block-heading"><span id="toc15">▷ゴール作成</span></h4>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>パーティクルシステムを作成して位置を(0, 0, 3)とし、その場に留まって揺らめくエフェクトとします。</p>



<figure class="wp-block-image size-full is-resized"><a href="https://d9m.one/wp-content/uploads/2022/07/TandH_022.png"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/07/TandH_022.png" alt="" class="wp-image-839" width="658" height="475" srcset="https://d9m.one/wp-content/uploads/2022/07/TandH_022.png 877w, https://d9m.one/wp-content/uploads/2022/07/TandH_022-300x217.png 300w, https://d9m.one/wp-content/uploads/2022/07/TandH_022-768x554.png 768w" sizes="(max-width: 658px) 100vw, 658px" /></a><figcaption>ゴール作成</figcaption></figure>



<p class="has-medium-font-size"><strong>・パーティクルシステム作成</strong></p>



<ol class="wp-block-list"><li>ヒエラルキー左上の「＋」をクリック</li><li>「エフェクト＞パーティクルシステム」をクリック</li><li>「Goal」と入力し、ENTERキーを押す</li></ol>



<p class="has-medium-font-size"><strong><strong>・<strong>位置、回転、スケール設定</strong></strong></strong></p>



<ol class="wp-block-list"><li>インスペクターのTransformにある位置を(0, 0, 3)とする</li><li>インスペクターのTransformにある回転を(-90, 0, 0)とする</li><li>インスペクターのTransformにあるスケールを(1, 1, 1)とする</li></ol>



<p class="has-medium-font-size"><strong>・メインモジュール設定</strong></p>



<p>メインモジュールを以下のように設定します。</p>



<ol class="wp-block-list"><li>インスペクターにあるParticle Systemの「メインモジュール＞継続時間」を(1)に設定</li><li>インスペクターにあるParticle Systemの「メインモジュール＞開始時の生存期間」を(1)に設定</li><li>インスペクターにあるParticle Systemの「メインモジュール＞開始時の速度」を(0)に設定</li><li>インスペクターにあるParticle Systemの「メインモジュール＞開始時の色」右にあるカラーバーをクリックし、色ダイアログの16進数を(FFFF00)に設定</li><li>色ダイアログの右上にある「×」をクリック</li><li>インスペクターにあるParticle Systemの「メインモジュール＞重力モディファイア」を(-0.1）に設定</li></ol>



<p class="has-medium-font-size"><strong>・形状モジュール設定</strong></p>



<p>形状モジュールを以下のように設定します。</p>



<ol class="wp-block-list"><li>形状モジュールの「形状」を(スフィア)に設定</li><li>形状モジュールの「半径」を(0.3)に設定</li><li>形状モジュールの「半径の厚さ」を(0)に設定</li></ol>
</div>



<h4 class="wp-block-heading"><span id="toc16">▷ゴール認識</span></h4>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>タグ「Goal」を作成し、ゴールに割り当てます。</p>



<figure class="wp-block-image size-full is-resized"><a href="https://d9m.one/wp-content/uploads/2022/07/TandH_023.png"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/07/TandH_023.png" alt="" class="wp-image-841" width="658" height="475" srcset="https://d9m.one/wp-content/uploads/2022/07/TandH_023.png 877w, https://d9m.one/wp-content/uploads/2022/07/TandH_023-300x217.png 300w, https://d9m.one/wp-content/uploads/2022/07/TandH_023-768x554.png 768w" sizes="(max-width: 658px) 100vw, 658px" /></a><figcaption>ゴール認識</figcaption></figure>



<p class="has-medium-font-size"><strong>・タグ作成</strong></p>



<ol class="wp-block-list"><li>ヒエラルキーの「Goal」をクリック</li><li>インスペクター上部のタグドロップダウンをクリックし、「タグを追加&#8230;」をクリック</li><li>インスペクター「Tag &amp; Layers」にあるタグ欄右下の「＋」をクリック</li><li>「Goal」と入力してENTERを押す</li></ol>



<p class="has-medium-font-size"><strong>・タグ割り当て</strong></p>



<ol class="wp-block-list"><li>ヒエラルキーの「Goal」をクリック</li><li>インスペクター上部のタグドロップダウンをクリックし、「Goal」をクリック</li></ol>
</div>



<h4 class="wp-block-heading"><span id="toc17">▷ゴール制御</span></h4>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>タマとゴールが衝突した時にタマとヒシガタの動きが止まるようにCharCtrlを修正します。</p>



<figure class="wp-block-image size-full is-resized"><a href="https://d9m.one/wp-content/uploads/2022/07/TandH_024.png"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/07/TandH_024.png" alt="" class="wp-image-843" width="658" height="475" srcset="https://d9m.one/wp-content/uploads/2022/07/TandH_024.png 877w, https://d9m.one/wp-content/uploads/2022/07/TandH_024-300x217.png 300w, https://d9m.one/wp-content/uploads/2022/07/TandH_024-768x554.png 768w" sizes="(max-width: 658px) 100vw, 658px" /></a><figcaption>ゴール制御</figcaption></figure>



<p class="has-medium-font-size"><strong>・スクリプト編集開始</strong></p>



<ol class="wp-block-list"><li>プロジェクトの「Assets&gt;CharCtrl」をダブルクリック</li></ol>



<p class="has-medium-font-size"><strong>・スクリプト変数追加</strong></p>



<ol class="wp-block-list"><li>「移動中かどうか」の変数の前に「プレイ中かどうか」の変数を作成</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="CharCtrl" data-lang="C#"><code>    bool playFlag = true;       // プレイ中かどうか
</code></pre></div>



<p class="has-medium-font-size"><strong>・スクリプト関数修正</strong></p>



<ol class="wp-block-list"><li>FixedUpdate関数の先頭に以下の処理を追加</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="CharCtrl" data-lang="C#"><code>        // プレイ中でない場合は即関数を終了
        if (playFlag == false) return;
</code></pre></div>



<p class="has-medium-font-size"><strong>・スクリプト<strong>関数修正</strong></strong></p>



<ol class="wp-block-list"><li>FixedUpdate関数の最後に以下の処理を追加</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="CharCtrl" data-lang="C#"><code>        // 自分がタマで、かつゴールと重なった場合
        else if (tag == &quot;Player&quot; && obj != null && obj.tag == &quot;Goal&quot;)
        {
            // タマのplayFlagをfalseに設定
            playFlag = false;
        }
</code></pre></div>



<p class="has-medium-font-size"><strong>・スクリプト<strong>関数修正</strong></strong></p>



<ol class="wp-block-list"><li>GetObject関数内の「処理対象とするタグの一覧を作成」を以下のスクリプトのように修正</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="CharCtrl" data-lang="C#"><code>        // 処理対象とするタグの一覧を作成
        string[] tags = new string[] { &quot;Toge&quot;, &quot;Block&quot;, &quot;Jump&quot;, &quot;Goal&quot; };
</code></pre></div>



<p class="has-medium-font-size"><strong><strong>・スクリプト保存</strong></strong></p>



<ol class="wp-block-list"><li>CTRLを押しながらSキーを押す</li></ol>



<p class="has-medium-font-size"><strong><strong><strong><strong>・Unityに戻ってスクリプトをビルド</strong></strong></strong></strong></p>



<ol class="wp-block-list"><li>Unityエディタのタイトルをクリック</li></ol>



<p class="has-medium-font-size"><strong><strong>・プロジェクト保存</strong></strong></p>



<ol class="wp-block-list"><li>CTRLを押しながらSキーを押す</li></ol>
</div>



<h4 class="wp-block-heading"><span id="toc18">▷動作確認</span></h4>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>タマとゴールが衝突したらタマの動きが止まるかどうか、プレビューして確認します。</p>



<figure class="wp-block-image size-full is-resized"><a href="https://d9m.one/wp-content/uploads/2022/07/TandH_025.png"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/07/TandH_025.png" alt="" class="wp-image-844" width="658" height="475" srcset="https://d9m.one/wp-content/uploads/2022/07/TandH_025.png 877w, https://d9m.one/wp-content/uploads/2022/07/TandH_025-300x217.png 300w, https://d9m.one/wp-content/uploads/2022/07/TandH_025-768x554.png 768w" sizes="(max-width: 658px) 100vw, 658px" /></a><figcaption>動作確認</figcaption></figure>



<p class="has-medium-font-size"><strong>・プレビュー開始</strong></p>



<ol class="wp-block-list"><li>エディタ上中央の「▶」をクリック</li></ol>



<p class="has-medium-font-size"><strong><strong>・動作確認</strong></strong></p>



<ol class="wp-block-list"><li>タマとゴールが衝突したらタマの動きが止まることを確認</li></ol>



<p class="has-medium-font-size"><strong>・プレビュー終了</strong></p>



<ol class="wp-block-list"><li>エディタ上中央の「▶」をクリック</li></ol>
</div>



<h4 class="wp-block-heading"><span id="toc19">▷スクリプト記述例</span></h4>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>「タマとゴールが衝突したらタマの動きが止まる」スクリプトの記述例です。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="CharCtrl" data-lang="C#"><code>using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharCtrl : MonoBehaviour
{
    bool playFlag = true;       // プレイ中かどうか
    bool moveFlag = false;      // 移動中かどうか
    float moveFrame = 30;       // １歩に何フレームかけるか
    float frameCount = 0;       // 経過フレーム数

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        // マウスクリックされた場合
        if (Input.GetMouseButtonUp(0) == true)
        {
            // 現在の回転値を保存
            Quaternion rot = transform.rotation;
            // 上下左右のオブジェクトを取得
            transform.rotation = Quaternion.Euler(0, 0, 0);
            GameObject objU = GetObject(transform.position + transform.forward);
            GameObject objD = GetObject(transform.position - transform.forward);
            GameObject objL = GetObject(transform.position - transform.right);
            GameObject objR = GetObject(transform.position + transform.right);

            // 指定方向がジャンプ台かつ逆側がなにも無いかトゲの場合、オブジェクトをジャンプ台の逆側に向ける
            float dir = -1;
            if (objU != null && objU.tag == &quot;Jump&quot; && (objD == null || objD.tag == &quot;Toge&quot;)) dir = 180;
            if (objD != null && objD.tag == &quot;Jump&quot; && (objU == null || objU.tag == &quot;Toge&quot;)) dir = 0;
            if (objL != null && objL.tag == &quot;Jump&quot; && (objR == null || objR.tag == &quot;Toge&quot;)) dir = 90;
            if (objR != null && objR.tag == &quot;Jump&quot; && (objL == null || objL.tag == &quot;Toge&quot;)) dir = 270;
            if (dir != -1)
            {
                moveFlag = true;
                rot = Quaternion.Euler(0, dir, 0);
            }
            // 回転値を設定
            transform.rotation = rot;
        }
    }

    // 物理フレーム更新時に処理
    void FixedUpdate()
    {
        // プレイ中でない場合は即関数を終了
        if (playFlag == false) return;

        // タグ検索でタマとヒシガタの一覧を取得する
        GameObject[] tamas = GameObject.FindGameObjectsWithTag(&quot;Player&quot;);
        GameObject[] hisis = GameObject.FindGameObjectsWithTag(&quot;Enemy&quot;);

        // １歩分のフレームが経過するまで何もしない
        frameCount++;
        if (frameCount &lt; moveFrame) return;

        // 経過フレーム数を0初期化する
        frameCount = 0;

        // 移動していない場合は終了
        if (moveFlag == false) return;

        // 移動先に壁かジャンプ台があったら移動終了
        GameObject moveObj = GetObject(transform.position + transform.forward);
        if (moveObj != null && (moveObj.tag == &quot;Block&quot; || moveObj.tag == &quot;Jump&quot;))
        {
            moveFlag = false;
            return;
        }

        // 向いている方向に１メートル進む
        transform.position += transform.forward;

        // ヒシガタとタマが衝突したら両方削除
        if (tamas.Length != 0 && hisis.Length != 0)
        {
            // タマとヒシガタの位置が同じだった場合
            if (tamas[0].transform.position == hisis[0].transform.position)
            {
                // タマとヒシガタを削除
                Destroy(tamas[0]);
                Destroy(hisis[0]);
            }
        }

        // 重なったオブジェクトを取得
        GameObject obj = GetObject(transform.position);
        // トゲと重なったら自分とトゲを削除
        if (obj != null && obj.tag == &quot;Toge&quot;)
        {
            Destroy(gameObject);
            Destroy(obj);
        }
        // 自分がタマで、かつゴールと重なった場合
        else if (tag == &quot;Player&quot; && obj != null && obj.tag == &quot;Goal&quot;)
        {
            // タマのplayFlagをfalseに設定
            playFlag = false;
        }
    }

    // 指定位置にあるオブジェクトを取得
    GameObject GetObject(Vector3 pos)
    {
        // 小数点誤差を補正
        float posX = Mathf.Floor(pos.x + 0.5f);
        float posZ = Mathf.Floor(pos.z + 0.5f);
        // 処理対象とするタグの一覧を作成
        string[] tags = new string[] { &quot;Toge&quot;, &quot;Block&quot;, &quot;Jump&quot;, &quot;Goal&quot; };
        // タグを１つ１つ処理
        for (int i = 0; i &lt; tags.Length; i++)
        {
            // 指定タグのオブジェクト一覧を取得
            GameObject[] objs = GameObject.FindGameObjectsWithTag(tags[i]);
            // オブジェクトを１つ１つ処理
            for (int j = 0; j &lt; objs.Length; j++)
            {
                // 指定位置に存在するオブジェクトを返す
                Vector3 objPos = objs[j].transform.position;
                if (objPos.x == posX && objPos.z == posZ) return objs[j];
            }
        }

        // 指定位置にオブジェクトが無いのでNULLを返す
        return null;
    }
}
</code></pre></div>
</div>
</div>



<p>作成手順を書いた記事です。</p>



<p>　　　<a href="https://d9m.one/proc_tandh/" type="URL" id="https://d9m.one/proc_tandh">→紹介</a><br>　　　<a href="https://d9m.one/proc_tandh1/" type="URL" id="https://d9m.one/proc_tandh1">→１／３　キャラクター作成</a><br>今▷　<a href="https://d9m.one/proc_tandh2/" type="URL" id="https://d9m.one/proc_tandh2">→２／３　オブジェクト作成</a><br>次▷　<a href="https://d9m.one/proc_tandh3/" type="URL" id="https://d9m.one/proc_tandh3">→３／３　ゲーム仕上げ</a></p>



<p><a href="https://d9m.one/proc_tandh_full/" type="URL" id="https://d9m.one/proc_tandh_full">完全手順</a><br>※紹介と１～３の手順を１ページにまとめています。<br>　読み込みに時間がかかるかも知れません。</p>



<div style="height:16px" aria-hidden="true" class="wp-block-spacer"></div>
<p>投稿 <a href="https://d9m.one/proc_tandh2/">【基本操作】タマあんどヒシガタ～２／３　オブジェクト作成【パズル】</a> は <a href="https://d9m.one">だくまたゲーム制作ブログ</a> に最初に表示されました。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://d9m.one/proc_tandh2/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>【基本操作】タマあんどヒシガタ～１／３　キャラクター作成【パズル】</title>
		<link>https://d9m.one/proc_tandh1/</link>
					<comments>https://d9m.one/proc_tandh1/#respond</comments>
		
		<dc:creator><![CDATA[だくまた]]></dc:creator>
		<pubDate>Fri, 22 Jul 2022 09:34:00 +0000</pubDate>
				<category><![CDATA[Unity]]></category>
		<category><![CDATA[基本操作]]></category>
		<category><![CDATA[自作ゲーム]]></category>
		<category><![CDATA[チュートリアル]]></category>
		<guid isPermaLink="false">https://d9m.one/?p=945</guid>

					<description><![CDATA[<p>●タマ作成 最初にプレイヤーとして動かすタマを作成します。タマの隣にジャンプ台がある時に画面をクリックすると、ジャンプ台に押したように動き出します。まずは単純に、いきなり進み続けるように作成します。 ▷タマ作成 球を作成 [&#8230;]</p>
<p>投稿 <a href="https://d9m.one/proc_tandh1/">【基本操作】タマあんどヒシガタ～１／３　キャラクター作成【パズル】</a> は <a href="https://d9m.one">だくまたゲーム制作ブログ</a> に最初に表示されました。</p>
]]></description>
										<content:encoded><![CDATA[
<div style="height:16px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="has-background has-medium-font-size wp-block-heading" style="background-color:#e5f7fd"><span id="toc1">●タマ作成</span></h3>



<div style="height:16px" aria-hidden="true" class="wp-block-spacer"></div>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>最初にプレイヤーとして動かすタマを作成します。<br>タマの隣にジャンプ台がある時に画面をクリックすると、ジャンプ台に押したように動き出します。<br>まずは単純に、いきなり進み続けるように作成します。</p>



<h4 class="wp-block-heading"><span id="toc2">▷タマ作成</span></h4>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>球を作成してTransformをリセットし、青色設定したマテリアルを割り当てます。</p>



<figure class="wp-block-image size-full is-resized"><a href="https://d9m.one/wp-content/uploads/2022/07/TandH_000.png"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/07/TandH_000.png" alt="" class="wp-image-807" width="658" height="475" srcset="https://d9m.one/wp-content/uploads/2022/07/TandH_000.png 877w, https://d9m.one/wp-content/uploads/2022/07/TandH_000-300x217.png 300w, https://d9m.one/wp-content/uploads/2022/07/TandH_000-768x554.png 768w" sizes="(max-width: 658px) 100vw, 658px" /></a><figcaption>タマ作成</figcaption></figure>



<p class="has-medium-font-size"><strong>・球作成</strong></p>



<ol class="wp-block-list"><li>ヒエラルキー左上の「＋」をクリック</li><li>「3Dオブジェクト＞スフィア」をクリック</li><li>「Tama」と入力し、ENTERキーを押す</li></ol>



<p class="has-medium-font-size"><strong>・位置、回転、スケールリセット</strong></p>



<ol class="wp-block-list"><li>インスペクターのTransformにある縦の三点リーダーをクリック</li><li>リセットをクリック</li></ol>



<p class="has-medium-font-size"><strong>・マテリアル作成</strong></p>



<ol class="wp-block-list"><li>プロジェクトの「Assets」を右クリックし、「作成＞マテリアル」をクリック</li><li>「matTama」と入力し、ENTERキーを押す</li></ol>



<p class="has-medium-font-size"><strong>・マテリアル色設定</strong></p>



<ol class="wp-block-list"><li>インスペクターの「Main Maps&gt;アルベド＞カラーバー」をクリックし、色ダイアログの16進数を「0000FF」に設定</li><li>色ダイアログの右上にある「×」をクリック</li></ol>



<p class="has-medium-font-size"><strong>・マテリアル割り当て</strong></p>



<ol class="wp-block-list"><li>プロジェクトの「matTama」をヒエラルキーの「Tama」にドラッグ＆ドロップ</li></ol>
</div>



<h4 class="wp-block-heading"><span id="toc3">▷タマ制御</span></h4>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>スクリプトを作成してタマに割り当て、「タマを30フレームに1メートルの速さで動く」ようにスクリプトを記述します。</p>



<figure class="wp-block-image size-full is-resized"><a href="https://d9m.one/wp-content/uploads/2022/07/TandH_001.png"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/07/TandH_001.png" alt="" class="wp-image-808" width="658" height="475" srcset="https://d9m.one/wp-content/uploads/2022/07/TandH_001.png 877w, https://d9m.one/wp-content/uploads/2022/07/TandH_001-300x217.png 300w, https://d9m.one/wp-content/uploads/2022/07/TandH_001-768x554.png 768w" sizes="(max-width: 658px) 100vw, 658px" /></a><figcaption>タマ制御</figcaption></figure>



<p class="has-medium-font-size"><strong>・スクリプト作成</strong></p>



<ol class="wp-block-list"><li>プロジェクトの「Assets」を右クリックし、「作成＞C# スクリプト」をクリック</li><li>「CharCtrl」と入力し、ENTERキーを押す</li></ol>



<p class="has-medium-font-size"><strong>・スクリプト割り当て</strong></p>



<ol class="wp-block-list"><li>プロジェクトの「Assets&gt;CharCtrl」をヒエラルキーの「Tama」にドラッグ＆ドロップ</li></ol>



<p class="has-medium-font-size"><strong>・スクリプト編集開始</strong></p>



<ol class="wp-block-list"><li>プロジェクトの「Assets&gt;CharCtrl」をダブルクリック</li></ol>



<p class="has-medium-font-size"><strong>・スクリプト変数定義</strong></p>



<ol class="wp-block-list"><li>クラス定義内の先頭に以下の変数を追加</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="CharCtrl.cs" data-lang="C#"><code>    float moveFrame = 30;       // １歩に何フレームかけるか
    float frameCount = 0;       // 経過フレーム数
</code></pre></div>



<p class="has-medium-font-size"><strong>・スクリプト関数定義</strong></p>



<ol class="wp-block-list"><li>クラス定義内の最後に以下の関数を追加</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="CharCtrl.cs" data-lang="C#"><code>    // 物理フレーム更新時に処理
    void FixedUpdate()
    {
    }
</code></pre></div>



<p class="has-medium-font-size">・<strong>スクリプト処理</strong></p>



<ol class="wp-block-list" id="block-df2c71c8-85d2-454d-ba7a-555e235c2226"><li>FixedFrameに以下の処理を記述</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="CharCtrl.cs" data-lang="C#"><code>        // １歩分のフレームが経過するまで何もしない
        frameCount++;
        if (frameCount &lt; moveFrame) return;

        // 経過フレーム数を0初期化する
        frameCount = 0;

        // 向いている方向に１メートル進む
        transform.position += transform.forward;
</code></pre></div>



<p class="has-medium-font-size"><strong>・スクリプト保存</strong></p>



<ol class="wp-block-list"><li>CTRLを押しながらSキーを押す</li></ol>



<p class="has-medium-font-size"><strong>・Unityに戻ってスクリプトをビルド</strong></p>



<ol class="wp-block-list" id="block-d832c413-6256-49d3-b625-25f4086a75c5"><li>Unityエディタのタイトルをクリック</li></ol>
</div>



<h4 class="wp-block-heading"><span id="toc4">▷タマ認識</span></h4>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>スクリプトでタマを認識させるため、タグ「Player」をタマに割り当てます。</p>



<figure class="wp-block-image size-full is-resized"><a href="https://d9m.one/wp-content/uploads/2022/07/TandH_002.png"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/07/TandH_002.png" alt="" class="wp-image-809" width="658" height="475" srcset="https://d9m.one/wp-content/uploads/2022/07/TandH_002.png 877w, https://d9m.one/wp-content/uploads/2022/07/TandH_002-300x217.png 300w, https://d9m.one/wp-content/uploads/2022/07/TandH_002-768x554.png 768w" sizes="(max-width: 658px) 100vw, 658px" /></a><figcaption>タマ認識</figcaption></figure>



<p class="has-medium-font-size"><strong>・タグ割り当て</strong></p>



<ol class="wp-block-list" id="block-d832c413-6256-49d3-b625-25f4086a75c5"><li>ヒエラルキーの「Tama」をクリック</li><li>インスペクター上部のタグドロップダウンをクリックし、「Player」をクリック</li></ol>



<p class="has-medium-font-size"><strong><strong>・プロジェクト保存</strong></strong></p>



<ol class="wp-block-list"><li>CTRLを押しながらSキーを押す</li></ol>
</div>



<h4 class="wp-block-heading"><span id="toc5">▷動作確認</span></h4>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>タマが期待通り動くかどうか、プレビューして確認します。</p>



<figure class="wp-block-image size-full is-resized"><a href="https://d9m.one/wp-content/uploads/2022/07/TandH_003.png"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/07/TandH_003.png" alt="" class="wp-image-810" width="658" height="475" srcset="https://d9m.one/wp-content/uploads/2022/07/TandH_003.png 877w, https://d9m.one/wp-content/uploads/2022/07/TandH_003-300x217.png 300w, https://d9m.one/wp-content/uploads/2022/07/TandH_003-768x554.png 768w" sizes="(max-width: 658px) 100vw, 658px" /></a><figcaption>動作確認</figcaption></figure>



<p class="has-medium-font-size"><strong>・プレビュー</strong>開始</p>



<ol class="wp-block-list"><li>エディタ上中央の「▶」をクリック</li></ol>



<p class="has-medium-font-size"><strong>・動作確認</strong></p>



<ol class="wp-block-list"><li>奥に向かって１メートルごとに進むことを確認</li></ol>



<p class="has-medium-font-size"><strong>・プレビュー</strong>終了</p>



<ol class="wp-block-list"><li>エディタ上中央の「▶」をクリック</li></ol>
</div>



<h4 class="wp-block-heading"><span id="toc6">▷スクリプト記述例</span></h4>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>「タマを30フレームに1メートルの速さで動く」スクリプトの記述例です。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="CharCtrl.cs" data-lang="C#"><code>using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharCtrl : MonoBehaviour
{
    float moveFrame = 30;       // １歩に何フレームかけるか
    float frameCount = 0;       // 経過フレーム数

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    // 物理フレーム更新時に処理
    void FixedUpdate()
    {
        // １歩分のフレームが経過するまで何もしない
        frameCount++;
        if (frameCount &lt; moveFrame) return;

        // 経過フレーム数を0初期化する
        frameCount = 0;

        // 向いている方向に１メートル進む
        transform.position += transform.forward;
    }
}
</code></pre></div>
</div>



<div style="height:16px" aria-hidden="true" class="wp-block-spacer"></div>
</div>



<h3 class="has-background has-medium-font-size wp-block-heading" style="background-color:#e5f7fd"><span id="toc7">●ヒシガタ作成</span></h3>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<div style="height:16px" aria-hidden="true" class="wp-block-spacer"></div>



<p>次に、同じように動くキャラクター「ヒシガタ」を作成します。<br>タマと衝突すると両方とも消滅します。</p>



<h4 class="wp-block-heading"><span id="toc8">▷ヒシガタ作成</span></h4>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>箱を作成してTransformを「位置(0, 0, 0)、回転(0, 45, 0)、スケール(0.7, 1, 0.7)」とし、赤色設定したマテリアルを割り当てます。</p>



<figure class="wp-block-image size-full is-resized"><a href="https://d9m.one/wp-content/uploads/2022/07/TandH_004.png"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/07/TandH_004.png" alt="" class="wp-image-812" width="658" height="475" srcset="https://d9m.one/wp-content/uploads/2022/07/TandH_004.png 877w, https://d9m.one/wp-content/uploads/2022/07/TandH_004-300x217.png 300w, https://d9m.one/wp-content/uploads/2022/07/TandH_004-768x554.png 768w" sizes="(max-width: 658px) 100vw, 658px" /></a><figcaption>ヒシガタ作成</figcaption></figure>



<p class="has-medium-font-size"><strong>・箱作成</strong></p>



<ol class="wp-block-list"><li>ヒエラルキー左上の「＋」をクリック</li><li>「3Dオブジェクト＞キューブ」をクリック</li></ol>



<p class="has-medium-font-size"><strong>・位置、回転、スケール設定</strong></p>



<ol class="wp-block-list"><li>インスペクターのTransformにある位置を(0, 0, 0)とする</li><li>インスペクターのTransformにある回転を(0, 45, 0)とする</li><li>インスペクターのTransformにあるスケールを(0.7, 1, 0.7)とする</li></ol>



<p class="has-medium-font-size"><strong>・マテリアル作成</strong></p>



<ol class="wp-block-list"><li>プロジェクトの「Assets」を右クリックし、「作成＞マテリアル」をクリック</li><li>「matHisi」と入力し、ENTERキーを押す</li></ol>



<p class="has-medium-font-size"><strong>・マテリアル色設定</strong></p>



<ol class="wp-block-list"><li>インスペクターの「MainMaps&gt;アルベド＞カラーバー」をクリックし、色ダイアログの16進数を「FF0000」に設定</li><li>色ダイアログの右上にある「×」をクリック</li></ol>



<p class="has-medium-font-size"><strong>・マテリアル割り当て</strong></p>



<ol class="wp-block-list"><li>プロジェクトの「matHisi」をヒエラルキーの「Cube」にドラッグ＆ドロップ</li></ol>
</div>



<h4 class="wp-block-heading"><span id="toc9">▷ヒシガタ階層化</span></h4>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>空オブジェクトを作成してTransformを「位置(1, 0, 0)、回転(0, 270, 0)、スケール(1, 1, 1)」とし、赤色の箱を子供とします。</p>



<figure class="wp-block-image size-full is-resized"><a href="https://d9m.one/wp-content/uploads/2022/07/TandH_005.png"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/07/TandH_005.png" alt="" class="wp-image-813" width="658" height="475" srcset="https://d9m.one/wp-content/uploads/2022/07/TandH_005.png 877w, https://d9m.one/wp-content/uploads/2022/07/TandH_005-300x217.png 300w, https://d9m.one/wp-content/uploads/2022/07/TandH_005-768x554.png 768w" sizes="(max-width: 658px) 100vw, 658px" /></a><figcaption>ヒシガタ階層化</figcaption></figure>



<p class="has-medium-font-size"><strong>・空オブジェクト作成</strong></p>



<ol class="wp-block-list"><li>ヒエラルキー左上の「＋」をクリック</li><li>「空のオブジェクトを作成」をクリック</li><li>「Hisi」と入力し、ENTERキーを押す</li></ol>



<p class="has-medium-font-size"><strong>・階層設定</strong></p>



<ol class="wp-block-list"><li>ヒエラルキーの「Cube」をヒエラルキーの「Hisi」にドラッグ＆ドロップ</li></ol>



<p class="has-medium-font-size"><strong>・位置、回転、スケール設定</strong></p>



<ol class="wp-block-list"><li>ヒエラルキーの「Hisi」をクリック</li><li>インスペクターのTransformにある位置を(3, 0, 3)とする</li><li>インスペクターのTransformにある回転を(0, 270, 0)とする</li><li>インスペクターのTransformにあるスケールを(1, 1, 1)とする</li></ol>
</div>



<h4 class="wp-block-heading"><span id="toc10">▷ヒシガタ認識</span></h4>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>タグ「Enemy」を作成し、ヒシガタに割り当てます。</p>



<figure class="wp-block-image size-full is-resized"><a href="https://d9m.one/wp-content/uploads/2022/07/TandH_006.png"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/07/TandH_006.png" alt="" class="wp-image-814" width="658" height="475" srcset="https://d9m.one/wp-content/uploads/2022/07/TandH_006.png 877w, https://d9m.one/wp-content/uploads/2022/07/TandH_006-300x217.png 300w, https://d9m.one/wp-content/uploads/2022/07/TandH_006-768x554.png 768w" sizes="(max-width: 658px) 100vw, 658px" /></a><figcaption>ヒシガタ認識</figcaption></figure>



<p class="has-medium-font-size"><strong>・タグ作成</strong></p>



<ol class="wp-block-list" id="block-d832c413-6256-49d3-b625-25f4086a75c5"><li>ヒエラルキーの「Hisi」をクリック</li><li>インスペクター上部のタグドロップダウンをクリックし、「タグを追加&#8230;」をクリック</li><li>インスペクター「Tag &amp; Layers」にあるタグ欄右下の「＋」をクリック</li><li>「Enemy」と入力してENTERを押す</li></ol>



<p class="has-medium-font-size"><strong>・タグ割り当て</strong></p>



<ol class="wp-block-list" id="block-3f34234f-49e3-4ace-a1a6-ad25622ab28e"><li>ヒエラルキーの「Hisi」をクリック</li><li>インスペクターのタグドロップダウンをクリックし、「Enemy」をクリック</li></ol>
</div>



<h4 class="wp-block-heading"><span id="toc11">▷ヒシガタ制御</span></h4>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>ヒシガタにCharCtrlを割り当て、CharCtrlを「タマとヒシガタが衝突したら両方削除する」ように修正します。</p>



<figure class="wp-block-image size-full is-resized"><a href="https://d9m.one/wp-content/uploads/2022/07/TandH_007.png"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/07/TandH_007.png" alt="" class="wp-image-815" width="658" height="475" srcset="https://d9m.one/wp-content/uploads/2022/07/TandH_007.png 877w, https://d9m.one/wp-content/uploads/2022/07/TandH_007-300x217.png 300w, https://d9m.one/wp-content/uploads/2022/07/TandH_007-768x554.png 768w" sizes="(max-width: 658px) 100vw, 658px" /></a><figcaption>ヒシガタ制御</figcaption></figure>



<p class="has-medium-font-size"><strong>・スクリプト割り当て</strong></p>



<ol class="wp-block-list"><li>プロジェクトの「Assets&gt;CharCtrl」をヒエラルキーの「Hisi」にドラッグ＆ドロップ</li></ol>



<p class="has-medium-font-size"><strong>・スクリプト編集開始</strong></p>



<ol class="wp-block-list"><li>プロジェクトの「Assets&gt;CharCtrl」をダブルクリック</li></ol>



<p class="has-medium-font-size"><strong>・タグでオブジェクトを検索</strong></p>



<ol class="wp-block-list"><li>FixedUpdate関数の処理先頭に「タグ検索でタマとヒシガタの一覧を取得する」処理を追加</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="CharCtrl.cs" data-lang="C#"><code>        // タグ検索でタマとヒシガタの一覧を取得する
        GameObject[] tamas = GameObject.FindGameObjectsWithTag(&quot;Player&quot;);
        GameObject[] hisis = GameObject.FindGameObjectsWithTag(&quot;Enemy&quot;);
</code></pre></div>



<p class="has-medium-font-size"><strong>・衝突時にオブジェクトを削除</strong></p>



<ol class="wp-block-list"><li>移動処理の後に「ヒシガタとタマが衝突したら両方削除」する処理を追加</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="CharCtrl.cs" data-lang="C#"><code>        // ヒシガタとタマが衝突したら両方削除
        if (tamas.Length != 0 && hisis.Length != 0)
        {
            // タマとヒシガタの位置が同じだった場合
            if (tamas[0].transform.position == hisis[0].transform.position)
            {
                // タマとヒシガタを削除
                Destroy(tamas[0]);
                Destroy(hisis[0]);
            }
        }
</code></pre></div>



<p class="has-medium-font-size"><strong>・スクリプト保存</strong></p>



<ol class="wp-block-list"><li>CTRLを押しながらSキーを押す</li></ol>



<p class="has-medium-font-size"><strong>・Unityに戻ってスクリプトをビルド</strong></p>



<ol class="wp-block-list" id="block-d832c413-6256-49d3-b625-25f4086a75c5"><li>Unityエディタのタイトルをクリック</li></ol>



<p class="has-medium-font-size"><strong>・プロジェクト保存</strong></p>



<ol class="wp-block-list"><li>CTRLを押しながらSキーを押す</li></ol>
</div>



<h4 class="wp-block-heading"><span id="toc12">▷動作確認</span></h4>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>ヒシガタが期待通り動き、タマと衝突したら両方消えるかどうか、プレビューして確認します。</p>



<figure class="wp-block-image size-full is-resized"><a href="https://d9m.one/wp-content/uploads/2022/07/TandH_008.png"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/07/TandH_008.png" alt="" class="wp-image-816" width="658" height="475" srcset="https://d9m.one/wp-content/uploads/2022/07/TandH_008.png 877w, https://d9m.one/wp-content/uploads/2022/07/TandH_008-300x217.png 300w, https://d9m.one/wp-content/uploads/2022/07/TandH_008-768x554.png 768w" sizes="(max-width: 658px) 100vw, 658px" /></a><figcaption>動作確認</figcaption></figure>



<p class="has-medium-font-size"><strong>・プレビュー</strong>開始</p>



<ol class="wp-block-list"><li>エディタ上中央の「▶」をクリック</li></ol>



<p class="has-medium-font-size"><strong>・動作確認</strong></p>



<ol class="wp-block-list"><li>ヒシガタが左奥に向かって進むことを確認</li><li>タマとヒシガタがぶつかったら両方消えることを確認</li></ol>



<p class="has-medium-font-size"><strong>・プレビュー終了</strong></p>



<ol class="wp-block-list"><li>エディタ上中央の「▶」をクリック</li></ol>
</div>



<h4 class="wp-block-heading"><span id="toc13">▷スクリプト記述例</span></h4>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>「タマとヒシガタが衝突したら両方削除する」スクリプトの記述例です。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="CharCtrl.cs" data-lang="C#"><code>using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharCtrl : MonoBehaviour
{
    float moveFrame = 30;       // １歩に何フレームかけるか
    float frameCount = 0;       // 経過フレーム数

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    // 物理フレーム更新時に処理
    void FixedUpdate()
    {
        // タグ検索でタマとヒシガタの一覧を取得する
        GameObject[] tamas = GameObject.FindGameObjectsWithTag(&quot;Player&quot;);
        GameObject[] hisis = GameObject.FindGameObjectsWithTag(&quot;Enemy&quot;);

        // １歩分のフレームが経過するまで何もしない
        frameCount++;
        if (frameCount &lt; moveFrame) return;

        // 経過フレーム数を0初期化する
        frameCount = 0;

        // 向いている方向に１メートル進む
        transform.position += transform.forward;

        // ヒシガタとタマが衝突したら両方削除
        if (tamas.Length != 0 && hisis.Length != 0)
        {
            // タマとヒシガタの位置が同じだった場合
            if (tamas[0].transform.position == hisis[0].transform.position)
            {
                // タマとヒシガタを削除
                Destroy(tamas[0]);
                Destroy(hisis[0]);
            }
        }
    }
}
</code></pre></div>
</div>



<div style="height:16px" aria-hidden="true" class="wp-block-spacer"></div>
</div>



<h3 class="has-background has-medium-font-size wp-block-heading" style="background-color:#e5f7fd"><span id="toc14">●トゲ作成</span></h3>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<div style="height:16px" aria-hidden="true" class="wp-block-spacer"></div>



<p>動かないヒシガタである「トゲ」を作成します。<br>タマやヒシガタが衝突するとトゲとともに消滅します。</p>



<h4 class="wp-block-heading"><span id="toc15">▷トゲ作成</span></h4>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>同じ構成なので、ヒシガタをコピーして利用します。<br>名前を変更して位置を(1, 0, 3)とし、色を紫に変更します。</p>



<figure class="wp-block-image size-full is-resized"><a href="https://d9m.one/wp-content/uploads/2022/07/TandH_009.png"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/07/TandH_009.png" alt="" class="wp-image-817" width="658" height="475" srcset="https://d9m.one/wp-content/uploads/2022/07/TandH_009.png 877w, https://d9m.one/wp-content/uploads/2022/07/TandH_009-300x217.png 300w, https://d9m.one/wp-content/uploads/2022/07/TandH_009-768x554.png 768w" sizes="(max-width: 658px) 100vw, 658px" /></a><figcaption>トゲ作成</figcaption></figure>



<p class="has-medium-font-size"><strong>・オブジェクト複製</strong></p>



<ol class="wp-block-list"><li>ヒエラルキーの「Hisi」をクリック</li><li>CTRLを押しながらDキーを押す</li></ol>



<p class="has-medium-font-size"><strong>・オブジェクト名変更</strong></p>



<ol class="wp-block-list"><li>F2キーを押す</li><li>「Toge」と入力し、ENTERキーを押す</li></ol>



<p class="has-medium-font-size"><strong>・位置設定</strong></p>



<ol class="wp-block-list"><li>インスペクターのTransformにある位置を(1, 0, 3)に設定</li></ol>



<p class="has-medium-font-size"><strong>・アセット複製</strong></p>



<ol class="wp-block-list"><li>プロジェクトの「Assets&gt;matHisi」をクリック</li><li>CTRLを押しながらDキーを押す</li></ol>



<p class="has-medium-font-size">・<strong>アセット名変更</strong></p>



<ol class="wp-block-list"><li>F2キーを押す</li><li>「matToge」を入力し、ENTERキーを押す</li></ol>



<p class="has-medium-font-size"><strong>・マテリアル色設定</strong></p>



<ol class="wp-block-list"><li>インスペクターの「Main Maps&gt;アルベド＞カラーバー」をクリックし、色ダイアログの16進数を「FF00FF」に設定</li><li>色ダイアログの右上にある「×」をクリック</li></ol>



<p class="has-medium-font-size"><strong>・マテリアル割り当て</strong></p>



<ol class="wp-block-list"><li>ヒエラルキーの「Toge」左にある「▶」をクリック</li><li>プロジェクトの「Assets&gt;matToge」をヒエラルキーの「Toge&gt;Cube」にドラッグ＆ドロップ</li></ol>
</div>



<h4 class="wp-block-heading"><span id="toc16">▷トゲ認識</span></h4>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>タグ「Toge」を作成し、トゲに割り当てます。</p>



<figure class="wp-block-image size-full is-resized"><a href="https://d9m.one/wp-content/uploads/2022/07/TandH_010.png"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/07/TandH_010.png" alt="" class="wp-image-819" width="658" height="475" srcset="https://d9m.one/wp-content/uploads/2022/07/TandH_010.png 877w, https://d9m.one/wp-content/uploads/2022/07/TandH_010-300x217.png 300w, https://d9m.one/wp-content/uploads/2022/07/TandH_010-768x554.png 768w" sizes="(max-width: 658px) 100vw, 658px" /></a><figcaption>トゲ認識</figcaption></figure>



<p class="has-medium-font-size"><strong>・タグ作成</strong></p>



<ol class="wp-block-list"><li>ヒエラルキーの「Toge」をクリック</li><li>インスペクターのタグドロップダウンをクリックし、「タグを追加&#8230;」をクリック</li><li>インスペクター「Tag &amp; Layers」にあるタグ欄右下の「＋」をクリック</li><li>「Toge」と入力してENTERを押す</li></ol>



<p class="has-medium-font-size"><strong>・タグ割り当て</strong></p>



<ol class="wp-block-list"><li>ヒエラルキーの「Toge」をクリック</li><li>インスペクターのタグドロップダウンをクリックし、「Toge」をクリック</li></ol>
</div>



<h4 class="wp-block-heading"><span id="toc17">▷トゲ制御</span></h4>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>トゲは動かないのでCharCtrlスクリプトを外します。<br>「自分（タマかヒシガタ）がトゲと衝突した時に両方消える」ようにCharCtrlを修正します。<br>結果、トゲとタマ、もしくはトゲとヒシガタが衝突した時に両方が消えるようになります。</p>



<figure class="wp-block-image size-full is-resized"><a href="https://d9m.one/wp-content/uploads/2022/07/TandH_011.png"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/07/TandH_011.png" alt="" class="wp-image-820" width="658" height="475" srcset="https://d9m.one/wp-content/uploads/2022/07/TandH_011.png 877w, https://d9m.one/wp-content/uploads/2022/07/TandH_011-300x217.png 300w, https://d9m.one/wp-content/uploads/2022/07/TandH_011-768x554.png 768w" sizes="(max-width: 658px) 100vw, 658px" /></a><figcaption>トゲ制御</figcaption></figure>



<p class="has-medium-font-size"><strong>・コンポーネント削除</strong></p>



<ol class="wp-block-list"><li>ヒエラルキーの「Toge」をクリック</li><li>インスペクターの「Char Ctrl(スクリプト)」にある縦の三点リーダーをクリック</li><li>「コンポーネントを削除」をクリック</li></ol>



<p class="has-medium-font-size"><strong>・スクリプト編集開始</strong></p>



<ol class="wp-block-list"><li>プロジェクトの「Assets&gt;CharCtrl」をダブルクリック</li></ol>



<p class="has-medium-font-size"><strong>・スクリプト関数定義</strong></p>



<ol class="wp-block-list"><li>FixedUpdate関数の後に以下の関数を追加</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="CharCtrl.cs" data-lang="C#"><code>    // 指定位置にあるオブジェクトを取得
    GameObject GetObject(Vector3 pos)
    {
        // 指定位置にオブジェクトが無いのでNULLを返す
        return null;
    }
</code></pre></div>



<p class="has-medium-font-size"><strong>・スクリプト関数作成</strong></p>



<ol class="wp-block-list"><li>GetObject内の「指定位置にオブジェクトが無いのでNULLを返す」前に以下の処理を追加</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="CharCtrl.cs" data-lang="C#"><code>        // 小数点誤差を補正
        float posX = Mathf.Floor(pos.x + 0.5f);
        float posZ = Mathf.Floor(pos.z + 0.5f);
        // 処理対象とするタグの一覧を作成
        string[] tags = new string[] { &quot;Toge&quot; };
        // タグを１つ１つ処理
        for (int i = 0; i &lt; tags.Length; i++)
        {
            // 指定タグのオブジェクト一覧を取得
            GameObject[] objs = GameObject.FindGameObjectsWithTag(tags[i]);
            // オブジェクトを１つ１つ処理
            for (int j = 0; j &lt; objs.Length; j++)
            {
                // 指定位置に存在するオブジェクトを返す
                Vector3 objPos = objs[j].transform.position;
                if (objPos.x == posX && objPos.z == posZ) return objs[j];
            }
        }
</code></pre></div>



<p class="has-medium-font-size"><strong>・スクリプト関数呼び出し</strong></p>



<ol class="wp-block-list"><li>FixedUpdate関数の最後に以下の処理を記述</li></ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="CharCtrl.cs" data-lang="C#"><code>        // 重なったオブジェクトを取得
        GameObject obj = GetObject(transform.position);
        // トゲと重なったら自分とトゲを削除
        if (obj != null && obj.tag == &quot;Toge&quot;)
        {
            Destroy(gameObject);
            Destroy(obj);
        }
</code></pre></div>



<p class="has-medium-font-size"><strong>・スクリプト保存</strong></p>



<ol class="wp-block-list"><li>CTRLを押しながらSキーを押す</li></ol>



<p class="has-medium-font-size"><strong>・Unityに戻ってスクリプトをビルド</strong></p>



<ol class="wp-block-list"><li>Unityエディタのタイトルをクリック</li></ol>



<p class="has-medium-font-size"><strong>・プロジェクト保存</strong></p>



<ol class="wp-block-list"><li>CTRLを押しながらSキーを押す</li></ol>
</div>



<h4 class="wp-block-heading"><span id="toc18">▷動作確認</span></h4>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>ヒシガタがトゲと衝突したら両方消えるかどうか、プレビューして確認します。</p>



<figure class="wp-block-image size-full is-resized"><a href="https://d9m.one/wp-content/uploads/2022/07/TandH_012.png"><img decoding="async" src="https://d9m.one/wp-content/uploads/2022/07/TandH_012.png" alt="" class="wp-image-821" width="658" height="475" srcset="https://d9m.one/wp-content/uploads/2022/07/TandH_012.png 877w, https://d9m.one/wp-content/uploads/2022/07/TandH_012-300x217.png 300w, https://d9m.one/wp-content/uploads/2022/07/TandH_012-768x554.png 768w" sizes="(max-width: 658px) 100vw, 658px" /></a><figcaption>動作確認</figcaption></figure>



<p class="has-medium-font-size"><strong>・プレビュー開始</strong></p>



<ol class="wp-block-list"><li>エディタ上中央の「▶」をクリック</li></ol>



<p class="has-medium-font-size"><strong>・動作確認</strong></p>



<ol class="wp-block-list"><li>ヒシガタとトゲがぶつかったら両方消えることを確認</li></ol>



<p class="has-medium-font-size"><strong>・プレビュー終了</strong></p>



<ol class="wp-block-list"><li>エディタ上中央の「▶」をクリック</li></ol>



<p class="has-medium-font-size"><strong>・プレビュー開始</strong></p>



<ol class="wp-block-list"><li>エディタ上中央の「▶」をクリック</li></ol>



<p class="has-medium-font-size"><strong>・プレビュー一時停止</strong></p>



<ol class="wp-block-list"><li>エディタ上中央の「||」をクリック</li></ol>



<p class="has-medium-font-size"><strong>・仮編集</strong></p>



<ol class="wp-block-list"><li>ヒエラルキーの「Toge」をクリック</li><li>インスペクターのTransformにある位置を(0, 0, 2)に設定</li></ol>



<p class="has-medium-font-size"><strong>・プレビュー再開</strong></p>



<ol class="wp-block-list"><li>エディタ上中央の「||」をクリック</li></ol>



<p class="has-medium-font-size"><strong>・動作確認</strong></p>



<ol class="wp-block-list"><li>タマとトゲがぶつかったら両方消えることを確認</li></ol>



<p class="has-medium-font-size"><strong>・プレビュー終了</strong></p>



<ol class="wp-block-list"><li>エディタ上中央の「▶」をクリック</li></ol>
</div>



<h4 class="wp-block-heading"><span id="toc19">▷スクリプト記述例</span></h4>



<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<p>「トゲと重なったら自分とトゲを削除する」スクリプトの記述例です。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-csharp" data-file="CharCtrl.cs" data-lang="C#"><code>using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharCtrl : MonoBehaviour
{
    float moveFrame = 30;       // １歩に何フレームかけるか
    float frameCount = 0;       // 経過フレーム数

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    // 物理フレーム更新時に処理
    void FixedUpdate()
    {
        // タグ検索でタマとヒシガタの一覧を取得する
        GameObject[] tamas = GameObject.FindGameObjectsWithTag(&quot;Player&quot;);
        GameObject[] hisis = GameObject.FindGameObjectsWithTag(&quot;Enemy&quot;);

        // １歩分のフレームが経過するまで何もしない
        frameCount++;
        if (frameCount &lt; moveFrame) return;

        // 経過フレーム数を0初期化する
        frameCount = 0;

        // 向いている方向に１メートル進む
        transform.position += transform.forward;

        // ヒシガタとタマが衝突したら両方削除
        if (tamas.Length != 0 && hisis.Length != 0)
        {
            // タマとヒシガタの位置が同じだった場合
            if (tamas[0].transform.position == hisis[0].transform.position)
            {
                // タマとヒシガタを削除
                Destroy(tamas[0]);
                Destroy(hisis[0]);
            }
        }

        // 重なったオブジェクトを取得
        GameObject obj = GetObject(transform.position);
        // トゲと重なったら自分とトゲを削除
        if (obj != null && obj.tag == &quot;Toge&quot;)
        {
            Destroy(gameObject);
            Destroy(obj);
        }
    }

    // 指定位置にあるオブジェクトを取得
    GameObject GetObject(Vector3 pos)
    {
        // 小数点誤差を補正
        float posX = Mathf.Floor(pos.x + 0.5f);
        float posZ = Mathf.Floor(pos.z + 0.5f);
        // 処理対象とするタグの一覧を作成
        string[] tags = new string[] { &quot;Toge&quot; };
        // タグを１つ１つ処理
        for (int i = 0; i &lt; tags.Length; i++)
        {
            // 指定タグのオブジェクト一覧を取得
            GameObject[] objs = GameObject.FindGameObjectsWithTag(tags[i]);
            // オブジェクトを１つ１つ処理
            for (int j = 0; j &lt; objs.Length; j++)
            {
                // 指定位置に存在するオブジェクトを返す
                Vector3 objPos = objs[j].transform.position;
                if (objPos.x == posX && objPos.z == posZ) return objs[j];
            }
        }

        // 指定位置にオブジェクトが無いのでNULLを返す
        return null;
    }
}
</code></pre></div>
</div>



<div style="height:16px" aria-hidden="true" class="wp-block-spacer"></div>
</div>



<p>作成手順を書いた記事です。</p>



<p>　　　<a href="https://d9m.one/proc_tandh/">→紹介</a><br>今▷　<a href="https://d9m.one/proc_tandh1/" type="URL" id="https://d9m.one/proc_tandh1">→１／３　キャラクター作成</a><br>次▷　<a href="https://d9m.one/proc_tandh2/" data-type="post" data-id="954">→２／３　オブジェクト作成</a><br>　　　<a href="https://d9m.one/proc_tandh3/" data-type="post" data-id="958">→３／３　ゲーム仕上げ</a></p>



<p><a href="https://d9m.one/proc_tandh_full/">完全手順</a><br>※紹介と１～３の手順を１ページにまとめています。<br>　読み込みに時間がかかるかも知れません。</p>



<div style="height:16px" aria-hidden="true" class="wp-block-spacer"></div>
<p>投稿 <a href="https://d9m.one/proc_tandh1/">【基本操作】タマあんどヒシガタ～１／３　キャラクター作成【パズル】</a> は <a href="https://d9m.one">だくまたゲーム制作ブログ</a> に最初に表示されました。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://d9m.one/proc_tandh1/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
