# Gyroscope Parallax Effect In Unity

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.

## 2 Answers

It's not so hard to find some sources from google;

Parallax effect (logic):
How do I implement parallax scrolling in 2D?

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);

• Why do you use eulerGyroAngles ? Why not just Quaternion – Drukalo Dec 7 '15 at 14:09
• @Drukalo If you know how to deal with them, go ahead. I personally prefer euler angles. – S. Tarık Çetin Dec 7 '15 at 14:34
• I mean i really don't know how to make object move with gyroscope like this youtube.com/watch?v=L4xuMh2rkJQ – Drukalo Dec 7 '15 at 16:40