- Home /
Question by
unity_-Uk1t88ScePqVA · Apr 27, 2021 at 07:25 AM ·
mobilecamera-movementcamera rotateaccelerometer
Rotate camera but view should be static
I am trying to rotate camera using phone accelerometer meter, but it is also rotating the view. I want the view to be static, attached the video of what i want to achieve. But if run the below code both and camera and view/scene rotates. Any help would be appreciated
using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;![alt text][1]
public class CamScript : MonoBehaviour
{
public GameObject target;
public Vector3 centerOffset;
public float sensitivity = 0;
public float horizontalRange = 60;
public float verticalRange = 0;
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(new Vector3(0f,0f,0f), Vector3.forward, xc);
}
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;
}
}
[1]: /storage/temp/179873-video-2.gif
video-2.gif
(282.5 kB)
Comment