facebook twitter hatena line email

「Unity/3d/collider/接触」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(サンプル)
行8: 行8:
 
Sphere.cs
 
Sphere.cs
 
<pre>
 
<pre>
 +
using UnityEngine;
 
public class Sphere : MonoBehaviour
 
public class Sphere : MonoBehaviour
 
{
 
{

2022年4月22日 (金) 15:22時点における版

サンプル

床にボールが、触れたときに、色を変える

  1. 3d/Planeを作成
  2. 3d/Sphereを作成し、AddComponentで、rigidbodyを追加
  3. 以下Sphere.csをSphereオブジェクトに追加

Sphere.cs

using UnityEngine;
public class Sphere : MonoBehaviour
{
    // 重なり始めたとき
    void OnCollisionEnter(Collision collision)
    {
    }
    // 重なり中のとき
    void OnCollisionStay(Collision collision)
    {
        if (collision.gameObject.name == "Plane")
        {
            GetComponent<Renderer>().material.color = Color.green;
        }
    }

    // 重なりから抜けたとき
    void OnCollisionExit(Collision collision)
    {
        if (collision.gameObject.name == "Plane")
        {
            GetComponent<Renderer>().material.color = Color.black;
        }
    }
}

参考:https://xr-hub.com/archives/5127

接触範囲を変更

  • SphereColliderのradiusを0.5から、1とかに変更すると、2倍の範囲で接触判定される。
  • もしくは、scaleを2とかに変えても、2倍の範囲で接触判定される。(こちらは、見た目も2倍)