[Unity Vuforia] Make an object move towards the camera
I'm trying to make a game that has augmented reality in it. I want to make a sphere come towards the vuforia camera when a player concludes a task that i already programmed, but i cant find out how to. I've tried using MoveTowards() but all it does is make it go up on a horizontal axis and not towards the camera on a vertical axis. Any help would be apreciated, thanks.
Answer by anilhdas · Mar 22, 2017 at 05:05 PM
To move the sphere towards the camera, you can follow the steps.
Rotate the object towards the camera using transform.lookAt().
Use Vector3.moveTowards() and pass the current position, target position (camera's position - minimum distance from camera, if any) and the speed of the movement.
I would be able to help more accurately if you can tell me some more details such as will you want the sphere to disappear when the image target is lost or after a fixed amount of time?
If the sphere you're trying to move, is attached to any Image target in Vuforia, I recommend to move it out as it might cause issues in the movement of the sphere when the image target is lost and found again.
You can then enable and disable the object (Sphere) by registering to Vuforia events to get the callback OnTrackingFound and OnTrackingLost.
Hope it helps. Let me know if you have any further queries.
Thats actually genius ^^ Thanks for replying as well! I can't test it today anymore but i'm gonna try it tomorrow and then ill get back with another reply Thanks again!
Answer by Lucas7CL · Mar 22, 2017 at 02:17 PM
Hi, maybe you can scale the model so can be seen as it´s nearer. Try using coordinates based on the camera as (0,0,0) then move along the z axis. Hope it helps!
Hey, thanks for the reply. The problem with me scaling the model to give the ilusion that its nearer is that im working with augmented reality, so if i look at it from the side it will still be on that same horizontal plane. :\ As far as moving the object in the z axis, how would i do that? Im still new to unity so im still very noobish at it, sorry! ^^ Thanks again for the help tho.
I will use a "ghost entity" one level up in the hierarchy of the 3D model. That entity you need to make it look to the camera always, use a script to do it. So, then when you need to move the object towards to camera you move along the z axis. If needed that object can be rotated and then use the moveTowards function. The trick is to use the ghost entity with script that makes look towards the camera, also you can scale it easly. Cheers!