Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

angle between two points unity

// Get Angle in Radians.
float AngleRad = Mathf. Atan2(Vect1. y - Vect2. y, Vect1. x - Vect2. x);
// Get Angle in Degrees.
float AngleDeg = (180 / Mathf. PI) * AngleRad;
Comment

unity get angle between two vectors

Small helper script to check angle between 2 objects in degrees (and in between 0-360).

It also includes test code for atan2Approximation, have not measured if there are any benefits using it..
Also note [ExecuteInEditMode], so it runs in editor without playmode.


using UnityEngine;
using System.Collections;

// helper script for testing angle calculation
// USAGE:
// – Attach this script to objectA and assign objectB as target
// – Then select objectA and move it around ObjectB and you can see angle values in inspector

[ExecuteInEditMode]
public class GetAngle : MonoBehaviour 
{
	public Transform target;
	public float angle;
	public float angleOpt;

	void Update () 
	{
		if (!target) return;

		var myPos = transform.position;
		myPos.y = 0;

		var targetPos = target.position;
		targetPos.y = 0;

		Vector3 toOther = (myPos – targetPos).normalized;

		angle = Mathf.Atan2(toOther.z, toOther.x) * Mathf.Rad2Deg + 180;
		angleOpt = atan2Approximation(toOther.z, toOther.x) * Mathf.Rad2Deg + 180;

		Debug.DrawLine (myPos, targetPos, Color.yellow);
	}


	float atan2Approximation(float y, float x) // http://http.developer.nvidia.com/Cg/atan2.html
	{
		float t0, t1, t2, t3, t4;
		t3 = Mathf.Abs(x);
		t1 = Mathf.Abs(y);
		t0 = Mathf.Max(t3, t1);
		t1 = Mathf.Min(t3, t1);
		t3 = 1f / t0;
		t3 = t1 * t3;
		t4 = t3 * t3;
		t0 =         – 0.013480470f;
		t0 = t0 * t4 + 0.057477314f;
		t0 = t0 * t4 – 0.121239071f;
		t0 = t0 * t4 + 0.195635925f;
		t0 = t0 * t4 – 0.332994597f;
		t0 = t0 * t4 + 0.999995630f;
		t3 = t0 * t3;
		t3 = (Mathf.Abs(y) > Mathf.Abs(x)) ? 1.570796327f–t3 : t3;
		t3 = (x < 0) ? 3.141592654f–t3:t3;
		t3 = (y < 0) ? –t3 : t3;
		return t3;
	}

}
Comment

PREVIOUS NEXT
Code Example
Typescript :: No provider for ChildrenOutletContexts 
Typescript :: ion slides updateAutoHeight 
Typescript :: set localStorage angualr 
Typescript :: sort list of list 
Typescript :: start blender from terminal 
Typescript :: ion input ngmodel not working ionic 6 
Typescript :: what is the name of belt around the orbits of earth and mars 
Typescript :: crashlytics ionic 3 
Typescript :: foreach on dictionary in typescript 
Typescript :: type of children for nextjs 
Typescript :: whats the power house of the cell 
Typescript :: typescript document.queryselector type 
Typescript :: how to generate controllers in nest js 
Typescript :: serving vue3 in django 
Typescript :: how to send data between components in react using link 
Typescript :: group objects in javascript 
Typescript :: typescript for 
Typescript :: must_not exists elastic search 
Typescript :: angular forkjoin 
Typescript :: java list of objects example 
Typescript :: typescript in node 
Typescript :: get key of enum typescript 
Typescript :: array contains typescript 
Typescript :: How to fix warning "function -- makes the dependencies of useEffect Hook change on every render"? 
Typescript :: node fetch image to base64 
Typescript :: typescript extend interface 
Typescript :: typescript with node on mac 
Typescript :: use toasts in django 
Typescript :: python requests no follow redirect 
Typescript :: typescript object to array 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =