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

unity distance between 2 vectors 2d

Vector2.Distance(vector1.position, vector2.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 :: longest substring without repeating characters leetcode 
Csharp :: add list to list c# 
Csharp :: C# max rand 
Csharp :: unity singleton 
Csharp :: frustum 
Csharp :: expando object c# 
Csharp :: serilog .net 6 
Csharp :: working with registry in c# 
Csharp :: C# xamaring form change text on label 
Csharp :: remove scenedelegate 
Csharp :: c# tab select tab 
Csharp :: math with c sharp 
Csharp :: how to populate list in c# 
Csharp :: C# 1 minute delay 
Csharp :: C# System.nanoTime 
Csharp :: c# read excel file using epplus save to datatable 
Csharp :: run in new thread C# 
Csharp :: How do I allow edit only a particular column in datagridview in windows application 
Csharp :: int c = new int(); in C# 
Csharp :: translate int to string with x 0 before c# 
Csharp :: unity rigidbody freeze all rotation 
Csharp :: c# object is enum 
Csharp :: Getting the text from a drop-down box 
Csharp :: c# if string in licbox 
Csharp :: c# draggable controls 
Csharp :: c# how to get a file path from user 
Csharp :: vb.net windows version check 
Csharp :: c# webbrowser write html to text file 
Csharp :: unity navmeshagent set destination 
Csharp :: escape chars for regex c# 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =