
アイテムとの衝突を検知する
Tutorial
Beginner
+10XP
15 mins
(50)
Unity Technologies

本チュートリアルでは、以下のことを行います:
- PlayerController のスクリプトを修正して、PickUp ゲームオブジェクトが Player スフィアに衝突したときに消えるようにする
- PickUp ゲームオブジェクトにタグをつけて、それだけが消えるように条件付きステートメントを作成する
- プレハブの PickUp Collider(ピックアップコライダー)をトリガーとして設定し、Rigidbody コンポーネントを追加することで、収集アイテムが正しく動作するようにする
Resources
1. OnTriggerEnter 関数で PickUps を無効にする
2. PickUp プレハブにタグを追加する
3. 条件付きステートメントの作成
4. Pickup Colliders をトリガーとして設定する
5. PickUp プレハブに Rigidbody コンポーネントを追加
PlayerController スクリプト
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
public float speed = 0;
private Rigidbody rb;
private float movementX;
private float movementY;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
private void OnMove(InputValue movementValue)
{
Vector2 movementVector = movementValue.Get<Vector2>();
movementX = movementVector.x;
movementY = movementVector.y;
}
private void FixedUpdate()
{
Vector3 movement = new Vector3(movementX, 0.0f, movementY);
rb.AddForce(movement * speed);
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("PickUp"))
{
other.gameObject.SetActive(false);
}
}
}