【问题标题】:I control the gameobject by tilting the phone and need to keep it balanced in whatever the rotation of the phone is when game starts我通过倾斜手机来控制游戏对象,并且需要在游戏开始时手机旋转时保持平衡
【发布时间】:2021-11-15 04:39:39
【问题描述】:

我上周发布了一款名为 Rotate Ball Pro 的游戏。但也有人告诉我,玩这个游戏不太舒服。 我使用统一和 C# 进行编码。我的游戏关卡上有迷宫和一个球体。球体具有刚体组件,但迷宫没有。因此,您可以通过倾斜手机来控制迷宫,从而使球体滚动。但是您必须将手机与地面平行以保持迷宫平衡。例如,您不能仰卧或以任何角度握住手机进行游戏。所以这让玩起来很不舒服。 无论游戏开始时手机的旋转是什么,我都想保持迷宫的平衡。我在网上搜索并尝试了很多东西,但无法解决。谁能帮帮我?

下面是图片问题的解释:Problem

这里是直接游戏链接:Rotate Ball Pro

这是我的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]

public class NewMovementScript : MonoBehaviour
{
    public float multiplier = 0;
    void Start()
    {
        Input.gyro.enabled = true;
        Screen.sleepTimeout = SleepTimeout.NeverSleep;
    }
    void FixedUpdate()
    {
        var dir = Vector3.zero;
        dir.x = Input.acceleration.x * -1 * multiplier;
        dir.y = Input.acceleration.z * multiplier;
        dir.z = Input.acceleration.y * multiplier;
        transform.eulerAngles = new Vector3(dir.z, 0f, dir.x);
    }
}

【问题讨论】:

    标签: c# android unity3d


    【解决方案1】:

    Input.acceleration 是最后一帧和当前帧之间的位置变化,正如您已经指出的那样,它没有考虑初始状态。

    这也与旋转几乎没有关系。

    我认为对于您的使用,您宁愿简单地使用Input.gyro.attitude

    文档中的示例

    public class Example : MonoBehaviour
    {
        // Rotate the object to match the device's orientation
        // in space.
        void Update()
        {
            transform.rotation = Input.gyro.attitude;
        }
    }
    

    如果您的对象使用刚体,您可能更愿意使用

    public class Example : MonoBehaviour
    {
        private Rigidbody rigidbody;
    
        void Awake ()
        {
            rigidbody = GetComponent<Rigidbody>();
        }
    
        void FixedUpdate()
        {
            rigidbody.MoveRotation(Input.gyro.attitude);
         }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多