Unityにて先週からゲーム制作を始めた初心者です。
Unityの教科書 Unity 2022完全対応版という教科書に従って、2Dのテンプレを使ったprojectでゲームを作成しています。
プレイヤーとオブジェクトの当たり判定の検出が上手くいかず、調べて出てくるチェック項目は確認したのですが、原因が分からず困っています。
何が問題かを教えて頂けますでしょうか?
以下がチェックした項目です。
・プレイヤーの設定(pngファイルに下記コンポーネントを追加)
└Rigidbody2D:BodyType⇒dynamic、Simulated⇒✔あり
└Box Collider 2D⇒Is Trigger✔なし
└Circle Collider 2D⇒Is Trigger✔なし
└PlayerController script(後述)をアタッチ済、操作はできてます。
・オブジェクトの設定(pngファイルに下記コンポーネントを追加)
└Box Collider 2D⇒Is Trigger✔あり
・Layer Collision Matrix
└すべてに✔あり。
・プレイヤーの速度
└特に高速ではないはず...(教科書通り作り、横移動の遅いマリオくらいの動き)
プレイヤーを動かしているScriptは以下になります。
一番下の「//ゴールに到達」以降が上手く機能せず、該当のオブジェクトに触れてもDebugメッセージが出ません。
それよりも上の部分は教科書通りに機能しています。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
Rigidbody2D rigid2D;
float jumpForce = 680.0f;
float walkForce = 30.0f;
float maxWalkSpeed = 2.0f;
void Start()
{
Application.targetFrameRate = 60;
this.rigid2D = GetComponent<Rigidbody2D>();
}
void Update()
{ //ジャンプする if(Input.GetKeyDown(KeyCode.Space)) { this.rigid2D.AddForce(transform.up * this.jumpForce); } //左右移動 int key = 0; if(Input.GetKey(KeyCode.RightArrow)) key = 1; if(Input.GetKey(KeyCode.LeftArrow)) key = -1; //プレイヤの速度 float speedx = Mathf.Abs(this.rigid2D.velocity.x); //スピード制限 if(speedx < this.maxWalkSpeed) { this.rigid2D.AddForce(transform.right * key * this.walkForce); } //動く方向に応じて反転 if(key != 0) { transform.localScale = new Vector3(key,1,1); } //ゴールに到達 void OnTriggerEnter2D(Collider2D other) { Debug.Log("ゴール"); } }
}

回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。
2023/01/24 02:48 編集