Distance#
Math / Geometry / Distance Game Objects
Can Read
Cannot Write
Description#
Returns the distance between the positions of two Game Objects
Parameters#
| Name | Description |
|---|---|
| Game Object 1 | The Game Object the distance is measured from |
| Game Object 2 | The Game Object the distance is measured to |
Code#
using System;
using UnityEngine;
using VisualScript.Icons;
namespace VisualScript.Runtime
{
[Get]
[Title("Distance")]
[Category("Math/Geometry/Distance", "Game Objects")]
[Description("Returns the distance between the positions of two Game Objects")]
[Image(typeof(IconCompass), ColorTheme.Type.Yellow, typeof(BadgeCubeOutline))]
[Parameter("Game Object 1", "The Game Object the distance is measured from")]
[Parameter("Game Object 2", "The Game Object the distance is measured to")]
[Keywords("Vector", "Position", "Point", "Far", "Near", "Close", "Range")]
[Serializable]
public class TypeNumberMathGameObjectsDistance : TypeNumber
{
[SerializeField] private GetGameObject m_GameObject1 = new GetGameObject();
[SerializeField] private GetGameObject m_GameObject2 = new GetGameObject();
public override double GetPreview(GameObject source) => Distance(
this.m_GameObject1.GetPreview(source),
this.m_GameObject2.GetPreview(source)
);
public override double Get(Args args) => Distance(
this.m_GameObject1.Get(args),
this.m_GameObject2.Get(args)
);
private static double Distance(GameObject gameObject1, GameObject gameObject2)
{
if (gameObject1 == null || gameObject2 == null) return 0f;
return Vector3.Distance(
gameObject1.transform.position,
gameObject2.transform.position
);
}
public override string ToString()
{
return $"Distance ({this.m_GameObject1}, {this.m_GameObject2})";
}
protected override SubscriptionMode Subscription => SubscriptionMode.OnChange;
}
}