Albertkaruna
Albertkaruna

Reputation: 85

Object is not coming on top/center of the Vuforia marker in Hololens, How to solve it?

I want to show some object on top of the Vuforia Marker but it's coming beyond the marker. I want it on the center of the marker.

I used Unity Lerp function to force the object to come on top of the marker but still it has some deviations and it's coming anywhere on the marker.

  Transform child = transform.GetChild(0);
  child.parent = null;
  Vector3 _size = GetComponent<ImageTargetBehaviour>().ImageTarget.GetSize();
  Vector3 _centre = new Vector3((0 + _size.x) / 2, (0 + _size.y) / 2, (0 +   _size.z) / 2);
  Vector3 _depth = Vector3.Lerp(child.position,  Camera.main.transform.position, 0.72f);
  child.position = new Vector3(_depth.x, Mathf.Lerp(_depth.y, _centre.y, 0.03f), _depth.z);

Upvotes: 2

Views: 198

Answers (1)

derHugo
derHugo

Reputation: 90570

It looks like this is executed on the marker.

First of all make the GetComponent call and others only once!

// Would be even better if you directly reference this via the Inspector
[SerializeField] private ImageTargetBehaviour itb;

// Alsonthis can probably be done once
[SerializeField] private Transform child;

// And also this should be set once
[SerializeField] private Camera _camera;


private void Awake ()
{
    if(!itb) itb = GetComponent<ImageTargetBehaviour>();

    child = transform.GetChild(0);

    _camera = Camera.main;
}

then later depending on your needs you can simply do

// Depending on your needs it coukd also be  + transform.up 
child.position = transform.position - transform.forward * XY;

if you want it e.g. always in front of the target independ of the camera position (meaning simply the way the target is facing). In this case however you could simply set the relative (local position) via the Inspector once and it would always keep this offset since it is a child.

Or e.g.

// Move XY in the vector position->camera
child.position = transform.position - (transform.position - _camera.transform.position).normalized * XY;

If you want it always "in front" of the target meaning closer to the camera them the target depending on the camera position.

Or simply

child.position = transform.position;

which would equal

child.localPosition = Vector3.zero;

if you simply want it in the same position as the target. But again you could simply set it up correctly via the Inspector and you should be fine.


The line child.parent = null; kind of breaks this behavior .. not sure if this was intended but once it is no child anymore it will in future stay on that position. Furthermore your line GetChild(0) would then fail as well since you moved the child out of the hierarchy...

Upvotes: 1

Related Questions