一个引力地球的范例

using UnityEngine;
using System.Collections;

public class Magnetic : MonoBehaviour
{
    public LayerMask m_MagneticLayers; //定义层
    public Vector3 m_Position; //定义向量
    public float m_Radius; //定义半径
    public float m_Force; //定义力学

    void FixedUpdate ()
    {
        Collider[] colliders; //定义一个COL
        Rigidbody rigidbody; //定义一个刚体

        colliders = Physics.OverlapSphere (transform.position + m_Position, m_Radius, m_MagneticLayers); //检测在m_Radius是否有COL相交 可以做某个半径的响应
        foreach (Collider collider in colliders)
        {
            rigidbody = (Rigidbody) collider.gameObject.GetComponent (typeof (Rigidbody));
            if (rigidbody == null)
            {
                continue;
            }
            rigidbody.AddExplosionForce (m_Force * -1, transform.position + m_Position, m_Radius); //施加力
        }
    }

    void OnDrawGizmos() //绘制一个用于观察的虚拟体范围
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere (transform.position + m_Position, m_Radius);
    }
}

/*
var radius = 5.0;
var power = 10.0;
function Start ()
{
 // Applies an explosion force to all nearby rigidbodies
 var explosionPos : Vector3 = transform.position;
 var colliders : Collider[] = Physics.OverlapSphere (explosionPos, radius);

 for (var hit : Collider in colliders)
 {
  if (!hit)
  continue;

  if (hit.rigidbody)
  hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 3.0);
 }
}
*/

你可能感兴趣的:(一个引力地球的范例)