Setting up ARKit in unity
There is no plugin at the asset store! So go here: https://bitbucket.org/Unity-Technologies/unity-arkit-plugin
The zipped file looks like a Unity Package, but it isn’t! You should copy its content to your project “Assets” folder.
Unity will update
Unlike Vuforia, Arkit doesn’t create a new Camera object, but uses Unity’s
· Create an empty object named “Camera Parent”
· Move the “Main Camera” to “Camera Parent” – making it ‘child’.
o Main Camera – Inspector
§ Clear Flags: Depth Only
§ Culing Mask: Everything
§ Field of View: 60
§ Clipping Planes: Near: 0.1 / Far: 37.81
o Add component: Unity AR Video
§ Material: YUVMaterial (or any clear material)
o Add component: Unity AR Camera Near Far
· Create empty object: ARCameraManager
o Add component: Unity AR Camera Manager
§ Camera: Drag n drop the Main Camera
§ Plane Detection: Select if you want it to track planes on either horizontal or vertical or both. Sometimes you don’t want to track walls or the floor.
§ Get Point Cloud: toggle
§ Enable Light Estimator: Toggle
§ Enable Auto Focus: Toggle (YES!)
ARKit is setup. Now setup the example scene
· Create Empty Object “DebugViz”
o Inside it create another empty object named: PointFinder
§ Add component: Unity Point Cloud Example
· Increase Num Points to Show
· Point Cloud Prefab: (grab example asset) PointCloudPrefab
§ Add another empty object to DebugViz: PlaneFinder
· Add component: Unity AR Generate Plane
o Plane Prefab: debugPlanePrefab (also part of the example collection)
Build for IOS
· Player Settings
o Bundle Identifier: add your company name:
o Camera Usage Description: (Type whater you want to the user allow the use of the phone’s camera…..could be “ARBABY”…whatever.
o Target minimum iOS Version: 11.0 (it has to be 11.0 or above)
o Requires ARKit support: toggle
Open the project in XCode and go through all the usual Apple nuisance of adding team/your apple developer coder (automatic); connect a phone to your computer; authorize everything and if everything works you should have an icon with the Unity App in your phone.
Unity AR Hit teste example
· Create empty object: hit_parent
o Inside it create empty object: hit / OR…. Create a 3D object (exp: Cube) and ..
o Add the “Unity AR Hit test example” script
§ Open the script
This script allows to detect if a touch on the screen is hitting one of the AR objects.
Remove all the Update Unity part – from “#if” all the way to “else” and remove “endElse”. We don’t need it to run inside the unity player
Remove comments for all ARHitTestResultType.xxxxxxx (activating them)
Build/ Compile and Run….it sort of works. A cube showed up and it stays in there…once. But no interaction.
AR Final – Little Button
Little button is tiny little boy that lives inside a flower that only blooms when bathe in moonlight. He one of the character of a children’s book I want to write.
In the AR experience the user controls the moonlight. At first you have to lighten the flower until it blossoms, revealing a sleeping Little Button inside. He then wakes up and jumps from the flower eager to play with the user, who controls a spotlight representing moonlight. Little Button will follow the light and wherever he goes, flowers blossom on his wake. By moving him around, the player can create drawings and patterns with the flower, mixed with their real environment through the use of AR.
· Make character that moves wherever the player points.
· Make flowers bloom on his wake.
· Model the character, flower, flowers path.
· Animate Character
· Add sound and effects
· Add Music.
· Deploy/Build to ARKit – Iphone.
The following code allows to make the character rotate and then move toward a mouse.click position. Not exactly what I need, but still a good start.
public class CharController : MonoBehaviour
float rotSpeed = 5;
float speed =5;
bool moving = false;
// Use this for initialization
// Update is called once per frame
//if "Move()" is left running on the update, the object will jitter!
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//If you are not hitting anything change the "1000" value.
if (Physics.Raycast(ray, out hit, 1000))
targetPosition = hit.point;
//LookAt snaps toward the position direction. Not what I need
//Turning around without making it look in the air. Transform only the Y position. Turn around the Y axis, left and right. By finding the X and Y differences we get a position to rotate the character. It projects the target X to the same level as the character
lookAtTarget = new Vector3(targetPosition.x - transform.position.x, transform.position.y, targetPosition.z - transform.position.z);
//use a Quaternion function with the values from LookAtTarget. Rotation that has to occur in order to look at the target.
charRot = Quaternion.LookRotation(lookAtTarget);
moving = true;
//Moving the character...smoothly using "Slerp" - Slerp smooths values
//smoothly rotates character toward the target position. "Spherically interpolates between a and b by t. The parameter t is clamped to the range [0,1]
transform.rotation = Quaternion.Slerp(transform.rotation, charRot, rotSpeed * Time.deltaTime);
//move toward target
transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
//toggle bool moving to false. Too accurate, might get into problem. Must set a range of being "on target" means.
if (transform.position == targetPosition)
moving = false;
It does have a bug, where the character floats toward the camera if clicked upon. I thought that the code should cancel that, but it still floats. Going back to basics on raycasting to figure out why.
Now I have to update the code to behave more closely to what I need. Instead of moving where clicked, I want it to constantly follow the mouse…. –