Unity Rotate Foor using Input.acceleration

2.5k Views Asked by At

I am trying to rotate floor using Input.acceleration. I want to rotate floor around the point where player is standing and to limit my rotations at certain angles also stop at 0 if Input.acceleration is close to zero. considering that i'm newbie at game programming I've came up with this code:

using UnityEngine;
using System.Collections;

public class Tilt : MonoBehaviour {

    public float maxRotationAngle = 350;                    // max rotation right
    public float minRotationAngle = 10;                     // max rotation left 
    public float rotationSpeed = 20;                        //rotation speed
    public Transform rotateAround;                          //rotation point
    private bool stopRotation = false;                      //if this is true rotation stops
    private int stopDir;                                    //direction where rotation stops -1 equals left 0 center 1 right

    void Update () {

        int tiltDir = 0;                                    //input tilt direction
        float accel = Input.acceleration.x;                 //input tilt value
        float currentRotation = transform.eulerAngles.z;    //current rotation

        //set rotation direction
        if (accel > 0) {
            tiltDir = 1;
        }else if (accel < 0){
            tiltDir = -1;
        }

        //stop rotation left
        if (!stopRotation && (currentRotation < maxRotationAngle && currentRotation > 270)) {
            stopRotation = true;
            stopDir = -1;
        }
        //stop rotation right
        if (!stopRotation && (currentRotation > minRotationAngle && currentRotation < 270)) {
            stopRotation = true;
            stopDir = 1;
        }
        //allow rotation right
        if (stopRotation && stopDir < 0 && Input.acceleration.x > 0) {
            stopRotation = false;

        }
        //allow rotation left
        if (stopRotation && stopDir > 0 && Input.acceleration.x < 0) {
            stopRotation = false;
        }
        //stop rotation center
        if(!stopRotation  && currentRotation < 0.2 || (currentRotation > 359.8 && currentRotation < 360)){
            if(accel > -0.1 && accel < 0.1){
                stopRotation = true;
                stopDir = 0;
            }
        }
        //allow rotation from center
        if(stopRotation && stopDir == 0 && (accel < -0.1 || accel > 0.1)){
            stopRotation = false;
        }
        //apply rotation
        if(!stopRotation){
            transform.RotateAround(rotateAround.position, new Vector3(0, 0, tiltDir), rotationSpeed * Mathf.Abs(accel) * Time.deltaTime);
        }

    }
}

This is working but this approach is not exact and i think there are cheaper ways of doing this. So Is there a better way?

1

There are 1 best solutions below

3
Nick Udell On

I came up with this, which I think accomplishes what you wanted, although I may have misread your code. I've removed your magic number, changed the angles to map between -180 and 180 and renamed your variables to have full names for better maintainability.

public float maxRotationAngle = 170;
public float minRotationAngle = -170;
public float minimumAcceleration = 0.1f;
public float rotationSpeed = 20;
public Transform rotateAroundTransform;

void Update () 
{
    float deltaAcceleration = Mathf.Abs(Input.acceleration.x);
    float currentRotation = transform.eulerAngles.z;

    //stop rotation outside of angle range and motion range
    if (currentRotation > minRotationAngle && 
        currentRotation < maxRotationAngle && 
        deltaAcceleration < minimumAcceleration) 
    {
        //set rotation direction
        int tiltDirection = Input.acceleration.x > 0 ? 1 : -1;
        transform.RotateAround(rotateAroundTransform.position, new Vector3(0, 0, tiltDirection), rotationSpeed * deltaAcceleration * Time.deltaTime);
    }
}

Hope that helps!