Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

unity get distance between two objects

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

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

how to compare distance between 2 objects unity

float distance = Vector3.Distance (object1.transform.position, object2.transform.position);
Comment

distance between two objects 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);
}
Comment

unity get distance between two objects

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

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

how to compare distance between 2 objects unity

float distance = Vector3.Distance (object1.transform.position, object2.transform.position);
Comment

distance between two objects 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);
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: unity 2d how to set an object or the mouse position 
Csharp :: c# center text 
Csharp :: c# string to memorystream 
Csharp :: kotlin random number 
Csharp :: wpf bind to self 
Csharp :: using variables from other procedures C# 
Csharp :: get unix time in seconds C# 
Csharp :: for loop unity 
Csharp :: random bool c# 
Csharp :: c# how to simulate mouse click 
Csharp :: how to change a image with code unity 
Csharp :: unity textmeshpro 
Csharp :: unity change sprite source image 
Csharp :: unity override 
Csharp :: c# if debug 
Csharp :: c# summary link 
Csharp :: c# request.url 
Csharp :: random value in array c# 
Csharp :: c# reverse string 
Csharp :: c# socket bind to localhost 
Csharp :: how to make rb.addforce 2d 
Csharp :: get filename from path c# 
Csharp :: subtract two times c# 
Csharp :: how to create a new folder with c# 
Csharp :: q# hello world 
Csharp :: game object disapear after transform.position 
Csharp :: c# conver date utc to cst 
Csharp :: how to do if comands in c# 
Csharp :: action being performed on this control is being called from the wrong thread c# 
Csharp :: unity deadzone 
ADD CONTENT
Topic
Content
Source link
Name
4+1 =