I want to make a parallax effect in Unity 3D with gyroscope like the one on this site: http://matthew.wagerfield.com/parallax/
I found an asset, but it is too expensive.
So I need a script which I can set to the GameObject, so it would move depending phone's gyroscope.
3 Answers 3
It's not so hard to find some sources from google;
Parallax effect (logic):
How do I implement parallax scrolling in 2D?
Gyroscope:
http://docs.unity3d.com/ScriptReference/Gyroscope.html
http://docs.unity3d.com/ScriptReference/Input-gyro.html
You are asking like "Gimme a script dammit!". Well, i can't give you a script. But i can tell you how to write your own.
Parallax effect basically means moving background objects slower than foreground ones to create a fake 3D (or depth) effect.
So if you have a vector that represents foregound velocity like:
//values and names are example
Vector3 foregroundVelocity = new Vector3(10,0,0);
Than you can use these vectors for background velocity (not limited to these):
//values and names are example
Vector3 backgroundVelocity = new Vector3(7,0,0);
Vector3 evenMoreBackgroundVelocity = new Vector3(5,0,0);
Vector3 farFarAwayVelocity = new Vector3(3,0,0);
Vector3 rightInFrontOfHorizonLineVelocity = new Vector3(1,0,0);
Then you can get gyro input from Unity's own Gyrpscope class, as @AhmetZambak mentioned, and just move your sprites (or gameObjects) according to magnitudes of corresponding angles, like:
//pseoudocode
Vector3 eulerGyroAngles = /*your input reading code*/;
gameObject.transform.position = (eulerGyroAngles.y, eulerGyroAngles.x, 0) * (corresponding magnitude vector);
-
\$\begingroup\$ Why do you use eulerGyroAngles ? Why not just Quaternion \$\endgroup\$Drukalo– Drukalo2015年12月07日 14:09:16 +00:00Commented Dec 7, 2015 at 14:09
-
\$\begingroup\$ @Drukalo If you know how to deal with them, go ahead. I personally prefer euler angles. \$\endgroup\$starikcetin– starikcetin2015年12月07日 14:34:30 +00:00Commented Dec 7, 2015 at 14:34
-
\$\begingroup\$ I mean i really don't know how to make object move with gyroscope like this youtube.com/watch?v=L4xuMh2rkJQ \$\endgroup\$Drukalo– Drukalo2015年12月07日 16:40:26 +00:00Commented Dec 7, 2015 at 16:40
There is a free plugin for gyroscope parallax effects, it can move object position and/or rotate object in space via gyroscope input data as gravity vector or as a movement in space direction.