Search Unity

Camera rotation in maze game depending of accelerometer

Discussion in 'Physics' started by Natasa, Aug 27, 2015.

  1. Natasa

    Natasa

    Joined:
    May 28, 2013
    Posts:
    14
    I'm still newbie... I'm trying to develop simple maze game. I want to roll my ball through maze, but I have to rotate camera left or right, otherwise the player can't see what's behind while rolling the ball on the left or on the right.

    Code (CSharp):
    1. void Start ()
    2.     {
    3.         offset = transform.position - player.transform.position;
    4.     }
    5.  
    6.     void LateUpdate()
    7.     {
    8.         transform.position = player.transform.position + offset;
    9.     }

    I'm using low pass filter for accelerometer values:

    Code (CSharp):
    1. Vector3 lowpass()
    2.     {
    3.         float LowPassFilterFactor = AccelerometerUpdateInterval / LowPassKernelWidthInSeconds;
    4.         lowPassValue = Vector3.Lerp(lowPassValue, Input.acceleration, LowPassFilterFactor);
    5.         return lowPassValue;
    6.     }
    Accelerometer values are from -1 to 1 for each coordinate. Because of that I check my lowPassValue.x value and limit it. If it's positive ( >0.3 ), then the camera should turn right and if it's negative ( <-0.3 ) then camera should turn left.

    Code (CSharp):
    1.     Quaternion rotation = Quaternion.AngleAxis(45, Vector3.up* Time.deltaTime) ; // right
    2.     transform.rotation = rotation;
    3.  
    4.     Quaternion rotation = Quaternion.AngleAxis(-45, Vector3.up * Time.deltaTime) ; // left
    5.     transform.rotation = rotation;
    But then my offset doesn't work anymore, I can't see ball anymore. And camera rotation doesn't work as it should.
    Is there any better solution for this or I'm using the wrong function?

    Any help will be very appreciated!