1

I have one imageTarget, I track that imageTarget with ARCamera, I want to find angle X of imageTarget relative to ARCamera(mobile device), because I need to move my object(imageTarget) towards the camera. I am using Vuforia in Unity. If possible please provide me code sample, as I am new in Unity, don't know much. I am sharing you my effort here. I am using this code for calculating angle, I think it is not correct. Please review it. Please help me.

Transform dummy = new GameObject("Dummy").transform;
dummy.SetParent(trackableEventHandler.transform);
dummy.LookAt(Camera.main.transform, trackableEventHandler.transform.up);
float angle = Mathf.Atan2(dummy.forward.z, dummy.forward.x) - Mathf.Atan2(trackableEventHandler.transform.forward.z, trackableEventHandler.transform.forward.x);
angle *= Mathf.Rad2Deg;
text.text = angle.ToString();
AsthaUndefined
  • 1,111
  • 1
  • 11
  • 24
user3151301
  • 39
  • 1
  • 8

1 Answers1

0

Try this

Transform dummy = new GameObject("Dummy").transform;
dummy.SetParent(trackableEventHandler.transform);
dummy.LookAt(Camera.main.transform, trackableEventHandler.transform.up);
float angle = Vector3.Angle(Camera.main.transform.forward, dummy.position - Camera.main.transform.position);
text.text = angle.ToString();

Edit: For signed angles, use this instead

float angle = Vector3.SignedAngle(Camera.main.transform.forward, dummy.position - Camera.main.transform.position, Vector3.up);

For 2D signed angles, use this instead

float angle = Vector2.SignedAngle(Vector2(Camera.main.transform.forward.x, Camera.main.transform.forward.z), Vector2(dummy.positionx.x - Camera.main.transform.position.x, dummy.positionx.z - Camera.main.transform.position.z));
Ryolu
  • 523
  • 3
  • 16
  • It is showing only positive value of X, it shouldn't be negative and positive on different X-axis? @Ryolu – user3151301 Oct 25 '18 at 05:54
  • @user3151301 `Vector3.Angle` returns the absolute angle between the direction the camera is facing and the direction the dummy is from the camera – Ryolu Oct 25 '18 at 06:07
  • My ImageAsset will be robot so i want to move it right and left relative to ARCamera, i need only that angle so as per my understanding for moving left angle must be negative and for moving right the robot angle will be positive, please help me to find that angle. Thanks – user3151301 Oct 25 '18 at 06:13
  • Yeah, now I am getting positve and negative angles, but the problem is i move my mobile up down, it is also changing angle, i need to change angle when device moved left or right, angle must not change on moving up or down? can you please help regarding this? – user3151301 Oct 25 '18 at 08:42
  • Try changing `Vector3.up` to `dummy.up` instead – Ryolu Oct 25 '18 at 08:47
  • still same problem – user3151301 Oct 25 '18 at 08:58
  • @user3151301 when moving up or down, how much is the angle changing – Ryolu Oct 25 '18 at 09:21
  • In my case up and down must not affect the angle only moving left and right. – user3151301 Oct 25 '18 at 09:42
  • @user3151301 how much does the angle change if you move 10cm upwards at a distance of 1m from the target – Ryolu Oct 25 '18 at 09:50
  • around 10 degree – user3151301 Oct 25 '18 at 10:09