Skip to content

Instantly share code, notes, and snippets.

@BichengLUO
Last active November 30, 2021 00:37
Show Gist options
  • Save BichengLUO/32091aecf9f189a24953840e32c060cf to your computer and use it in GitHub Desktop.
Save BichengLUO/32091aecf9f189a24953840e32c060cf to your computer and use it in GitHub Desktop.
A Unity3D script for rotating the camera according to the device's accelerometer
using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
public class GravityCamera : MonoBehaviour
{
public GameObject target;
public Vector3 centerOffset;
public float sensitivity = 1000;
public float horizontalRange = 60;
public float verticalRange = 30;
public int filterWindowSize = 5;
private Vector3 initialPosition;
private Quaternion initialRotation;
private Queue<Vector3> filter;
// Use this for initialization
void Start()
{
initialPosition = transform.position;
initialRotation = transform.rotation;
filter = new Queue<Vector3>();
}
// Update is called once per frame
void Update()
{
transform.position = initialPosition;
transform.rotation = initialRotation;
filter.Enqueue(Input.acceleration);
if (filter.Count > filterWindowSize)
filter.Dequeue();
float totalX = 0, totalY = 0;
foreach (Vector3 acc in filter)
{
totalX += acc.x;
totalY += acc.y;
}
float filteredX = totalX / filter.Count;
float filteredY = totalY / filter.Count;
float xc = -filteredX * horizontalRange;
float yc = (0.5f + filteredY) * 2 * verticalRange;
xc = Clamp(xc, -horizontalRange, horizontalRange);
yc = Clamp(yc, -verticalRange, verticalRange);
transform.RotateAround(target.transform.position + centerOffset, Vector3.up, xc);
transform.RotateAround(target.transform.position + centerOffset, Vector3.right, yc);
}
public T Clamp<T>(T val, T min, T max) where T : IComparable<T>
{
if (val.CompareTo(min) < 0) return min;
else if (val.CompareTo(max) > 0) return max;
else return val;
}
public void OnDisable()
{
transform.position = initialPosition;
transform.rotation = initialRotation;
}
/*
void OnGUI()
{
GUI.skin.label.fontSize = GUI.skin.box.fontSize = GUI.skin.button.fontSize = 20;
GUI.Box(new Rect(5, 5, 200, 40), String.Format("{0:0.000}", Input.acceleration.x));
GUI.Box(new Rect(5, 50, 200, 40), String.Format("{0:0.000}", Input.acceleration.y));
GUI.Box(new Rect(5, 95, 200, 40), String.Format("{0:0.000}", Input.acceleration.z));
float xc = -Input.acceleration.x * horizontalRange;
float yc = (0.5f + Input.acceleration.y) * 2 * verticalRange;
xc = Clamp(xc, -horizontalRange, horizontalRange);
yc = Clamp(yc, -verticalRange, verticalRange);
GUI.Box(new Rect(5, 150, 200, 40), String.Format("XC:{0:0.000}", xc));
GUI.Box(new Rect(5, 195, 200, 40), String.Format("YC:{0:0.000}", yc));
}
*/
}
@VarunKap0
Copy link

Hey! Great script, works really well.
The thing is, it rotates about an axis. Is there a way to make the scene move when the camera moves its transform?
Also, can I smoothen the motion?

Thanks :)

@spiniferx
Copy link

How to apply this script ??

@carter1990
Copy link

☄ AR Camera ACCELEROMETER – NEW UNITY ASSET

When it comes to augmented reality, it is usually implied that the user of your game or application has a modern device.

Moreover, such a smartphone should not be from the budget class itself. We need a gyroscope, and even a higher OS version and so on.

In budget devices, there is an accelerometer sensor, which is not designed to implement augmented reality. More precisely, it does not provide enough data.

It turns out that such users will not be able to use the AR-application. But if you really, really want, then you can give them such a chance.

I have developed an AR camera for Unity using an accelerometer.
Researchers say that it works on 90% of devices.

☄ Download: http://u3d.as/YSM
☄ Download: http://u3d.as/YSM
☄ Download: http://u3d.as/YSM

⛑ I answer any questions: info@makaka.org
⛑ Support: info@makaka.org

✏ Documentation: https://makaka.org/ar-camera-accelerometer/

ar-camera-accelerometer-screenshot-1

LOL BRO CHILL. You got all this free information from github and sold it all on the unity asset store and now you are advertising on free INFO? GTFOH

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment