陀螺儀代碼 網上有不少,2d,3d。 不過好像 就是少了 c# 2d 版本的。c#
因而我找了 網上代碼 改寫了下。測試
代碼都是很簡單的,很少作說明了ui
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class news : MonoBehaviour { public RectTransform go; private float cross_x, cross_y; private Vector2 vec; public Text tt; void Start() { print(go.sizeDelta.x); cross_x = Screen.width - go.sizeDelta.x/2; cross_y = Screen.height - go.sizeDelta.y/2; vec = go.position; } void Update() { vec.x += Input.acceleration.x*10; vec.y += Input.acceleration.y * 10; if (vec.x < go.sizeDelta.x/2) { vec.x = go.sizeDelta.x/2; } else if (vec.x > cross_x) { vec.x = cross_x; } if (vec.y < go.sizeDelta.y/2) { vec.y = go.sizeDelta.y/2; } else if (vec.y > cross_y) { vec.y = cross_y; } tt.text = vec.x + " " + vec.y; go.position = vec; } }
用手機測試,效果 能夠 讓 紅色方塊 移動url
另外,陀螺儀我發現用的人不多,早期有人用來 作小球滾動遊戲,後來就沒有了。還有一些人作成 3d ui,不過我以爲,這些用法都不對。3d
我有一臺 psv,有 背觸 功能。但真正能 結合 作出的 好遊戲的 只有一款 《撕紙小郵差》code
其實,陀螺儀 結合遊戲,不必定是 效果,也不必定是 方向控制。你們好好想一想,他實際上是 一個替代按鍵,讓玩家 無法騰出手的時候,選擇的一種 操做方式。orm
補充:blog
Console.WriteLine(Math.Atan2(x ,y) * 180 / Math.PI);