Unity 3D逼真的加速度计控制 [英] Unity 3D realistic accelerometer control

查看:765
本文介绍了Unity 3D逼真的加速度计控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何实现类似于此游戏的控件?
https:// play .google.com / store / apps / details?id = com.fridgecat.android.atiltlite& hl = zh_CN

How do we achieve a control similar to this game? https://play.google.com/store/apps/details?id=com.fridgecat.android.atiltlite&hl=en

推荐答案

您可以使用内置物理学来做到这一点:

You can do this with builtin physics:


  1. 用一些简单的可缩放立方体创建一个关卡(不要忘记地面)。


  1. 添加球-一个球体,然后添加 RigidBody 。在刚体上设置一个约束-检查冻结位置y(如果将设备倒置,它会跳出水平位置。)。

  1. add the ball - a sphere, then and add a RigidBody to it. Set a constraint on the rigidbody - check freeze position y (or it will be able to jump out of the level if you put the device upside down).

将此脚本添加到场景中的任何位置(例如,添加到摄像机):

add this script anywhere on the scene (for example to the camera):

using UnityEngine;

public class GravityFromAccelerometer : MonoBehaviour {

    // gravity constant
    public float g=9.8f;

    void Update() {
        // normalize axis
        Physics.gravity=new Vector3( 
            Input.acceleration.x,
            Input.acceleration.z,
            Input.acceleration.y
        )*g;
    }

}

或者如果您希望物理学只是影响这个对象,将此脚本添加到该对象并关闭它受重力影响的刚体:

or if you want the physics to just affect this one object, add this script to the object and turn off it's rigidbody's affected by gravity:

using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class ForceFromAccelerometer : MonoBehaviour {

    // gravity constant
    public float g=9.8f;

    void FixedUpdate() {
        // normalize axis
        var gravity = new Vector3 (
            Input.acceleration.x,
            Input.acceleration.z,
            Input.acceleration.y
        ) * g;

        GetComponent<Rigidbody>().AddForce (gravity, ForceMode.Acceleration);
    }

}


现在您有了工作球物理。要使球表现出所需的效果,请尝试使用刚体属性。例如,将阻力更改为0.1。

And now you have working ball physics. To make the ball behave as you'd like, try to play with the rigidbody properties. For example, change the drag to something like 0.1.

这篇关于Unity 3D逼真的加速度计控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆