facebook twitter hatena line email

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

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(接触範囲を変更)
(サンプル)
(同じ利用者による、間の2版が非表示)
行3: 行3:
  
 
#3d/Planeを作成
 
#3d/Planeを作成
#3d/Sphereを作成し、SphereColliderにリネームし、AddComponentで、rigidbodyを追加
+
#3d/Sphereを作成し、AddComponentで、rigidbodyを追加
#以下SphereCollider.csをSphereColliderオブジェクトに追加
+
#以下Sphere.csをSphereオブジェクトに追加
  
SphereCollider.cs
+
Sphere.cs
 
<pre>
 
<pre>
public class SphereCollider : MonoBehaviour
+
using UnityEngine;
 +
public class Sphere : MonoBehaviour
 
{
 
{
 
     // 重なり始めたとき
 
     // 重なり始めたとき
行38: 行39:
 
==接触範囲を変更==
 
==接触範囲を変更==
 
*SphereColliderのradiusを0.5から、1とかに変更すると、2倍の範囲で接触判定される。
 
*SphereColliderのradiusを0.5から、1とかに変更すると、2倍の範囲で接触判定される。
*もしくは、scaleを2とかに変えても、2倍の範囲で接触判定される。(見た目も2倍)
+
*もしくは、scaleを2とかに変えても、2倍の範囲で接触判定される。(こちらは、見た目も2倍)

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倍)