首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Input.acceleration为单位旋转

使用Input.acceleration为单位旋转
EN

Stack Overflow用户
提问于 2014-04-18 20:21:15
回答 1查看 2.5K关注 0票数 1

我正在试着用Input.acceleration旋转地板。我想围绕玩家站立的位置旋转地板,如果Input.acceleration接近于0,我想限制我在某些角度的旋转也停止在0。考虑到我是游戏编程的新手,我想出了这个代码:

代码语言:javascript
复制
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);
        }

    }
}

这是有效的,但这种方法并不准确,我认为有更便宜的方法来做到这一点。那么有没有更好的方法呢?

EN

回答 1

Stack Overflow用户

发布于 2014-04-22 20:35:07

我想出了这个,我认为它实现了您想要的东西,尽管我可能读错了您的代码。我删除了您的幻数,将角度更改为-180到180之间的映射,并将您的变量重命名为全名,以获得更好的可维护性。

代码语言:javascript
复制
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);
    }
}

希望这能有所帮助!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23154025

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档