“Jarak antara dua objek Unity 2D” Kode Jawaban

Jarak Persatuan antara 2 Vektor 2D

Vector2.Distance(vector1.position, vector2.position);
TebionDL

persatuan mendapatkan jarak antara dua objek

// Vector3.Distance is the same as (a-b).magnitude

float distance = Vector3.Distance(a.transform.position, b.transform.position);
AdobeZev

Jarak antara dua objek Unity 2D

public GameObject object1;
public GameObject object2;
void Update()
{
  Vector2 Pos1 = object1.transform.position;
  Vector2 Pos2 = object2.transform.position;
  float x1 = Pos1.x, x2 = Pos2.x, y1 = Pos1.y, y2 = Pos2.y;
  // Distance between X coordinates
  float xDif = Mathf.Abs((Mathf.Max(x1,x2) - Mathf.Min(x1,x2));
  // Distance between Y coordinates
  float xDif = Mathf.Abs((Mathf.Max(y1,y2) - Mathf.Min(y1,y2));
  // Pythagorean theorem
  float finalDistance = Mathf.Sqrt(xDif * xDif + yDif * yDif);
  Debug.Log("Distance Between Object1 And Object2 Is " + finalDistance);
}
Xixika

Jawaban yang mirip dengan “Jarak antara dua objek Unity 2D”

Pertanyaan yang mirip dengan “Jarak antara dua objek Unity 2D”

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya