Move/roll 带加速度计的球
Move/roll ball with accelerometer
所以我目前刚开始编程,对这个话题很陌生。
我确实是从 Unity 游戏引擎开始的;人们说这不是最好的开始方式,但无论如何。
我用unity的基础教程做了第一个游戏
虽然我还不能真正理解 C# 的复杂性。
(使用 Visual Studio,不确定我是否应该切换到 sublime 以及如何切换)
这个游戏是关于移动球和收集东西。
在 PC 上,它与方向键上的 AddForce 和 Vector3 移动配合得很好。虽然我想尝试为移动设备制作这款游戏,但我考虑过我可能会使用移动设备的陀螺仪而不是键入屏幕。我在 Unity API 文档中找到了 "gyro" 变量(?),但我真的不知道如何定义它只是为了移动 x 和 z 轴,所以球不会开始飞行关闭 table。我尝试使用加速器变量(?),但确实发生了这种情况,即使 y 轴设置为 0。
以下代码是我目前在 GameObject "player" 中的代码:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class AccelerometerInput : MonoBehaviour
{
public float speed;
public Text countText;
public Text winText;
private Rigidbody rb;
private int count;
void Start()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText();
winText.text = "";
}
private void Update()
{
transform.Translate(Input.gyro.x, 0, -Input.gyro.z);
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * speed);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Capsule"))
{
other.gameObject.SetActive(false);
count = count + 1;
SetCountText();
}
}
void SetCountText()
{
countText.text = "Count: " + count.ToString();
if (count >= 8)
{
winText.text = "You Win";
}
}
}
我对所有这一切都是新手,尤其是编码,任何能帮助我理解语言解释方式的东西都将不胜感激!
谢谢!
I did start with the Unity game engine, people say it's not the best
way to start but whatever.
没错。网上有很多 C# 教程。只要了解基本的 C# 知识,您就可以在 Unity 中使用。如果您不这样做,您使用 Unity 可以做的事情就会受到限制。
要回答您的问题,您需要 加速度计 而不是 陀螺仪 传感器。此外,从 Update 函数中删除 transform.Translate(Input.gyro.x, 0, -Input.gyro.z);
。 不要通过transform.Translate
移动带有Rigidbody
的对象,否则,您将运行陷入诸如无碰撞之类的问题。
应该这样做:
Vector3 movement = new Vector3(-Input.acceleration.y, 0f, Input.acceleration.x);
您仍然需要一种方法来检测您是为移动设备还是桌面设备构建。这可以通过 Unity 的预处理器 directives.
来完成
void FixedUpdate()
{
Vector3 movement = Vector3.zero;
//Mobile Devices
#if UNITY_IOS || UNITY_ANDROID || UNITY_WSA_10_0
movement = new Vector3(-Input.acceleration.y, 0.0f, Input.acceleration.x);
#else
//Desktop
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
movement = new Vector3(moveHorizontal, 0f, moveVertical);
#endif
rb.AddForce(movement * speed);
}
所以我目前刚开始编程,对这个话题很陌生。
我确实是从 Unity 游戏引擎开始的;人们说这不是最好的开始方式,但无论如何。
我用unity的基础教程做了第一个游戏
虽然我还不能真正理解 C# 的复杂性。 (使用 Visual Studio,不确定我是否应该切换到 sublime 以及如何切换)
这个游戏是关于移动球和收集东西。 在 PC 上,它与方向键上的 AddForce 和 Vector3 移动配合得很好。虽然我想尝试为移动设备制作这款游戏,但我考虑过我可能会使用移动设备的陀螺仪而不是键入屏幕。我在 Unity API 文档中找到了 "gyro" 变量(?),但我真的不知道如何定义它只是为了移动 x 和 z 轴,所以球不会开始飞行关闭 table。我尝试使用加速器变量(?),但确实发生了这种情况,即使 y 轴设置为 0。 以下代码是我目前在 GameObject "player" 中的代码:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class AccelerometerInput : MonoBehaviour
{
public float speed;
public Text countText;
public Text winText;
private Rigidbody rb;
private int count;
void Start()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText();
winText.text = "";
}
private void Update()
{
transform.Translate(Input.gyro.x, 0, -Input.gyro.z);
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * speed);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Capsule"))
{
other.gameObject.SetActive(false);
count = count + 1;
SetCountText();
}
}
void SetCountText()
{
countText.text = "Count: " + count.ToString();
if (count >= 8)
{
winText.text = "You Win";
}
}
}
我对所有这一切都是新手,尤其是编码,任何能帮助我理解语言解释方式的东西都将不胜感激! 谢谢!
I did start with the Unity game engine, people say it's not the best way to start but whatever.
没错。网上有很多 C# 教程。只要了解基本的 C# 知识,您就可以在 Unity 中使用。如果您不这样做,您使用 Unity 可以做的事情就会受到限制。
要回答您的问题,您需要 加速度计 而不是 陀螺仪 传感器。此外,从 Update 函数中删除 transform.Translate(Input.gyro.x, 0, -Input.gyro.z);
。 不要通过transform.Translate
移动带有Rigidbody
的对象,否则,您将运行陷入诸如无碰撞之类的问题。
应该这样做:
Vector3 movement = new Vector3(-Input.acceleration.y, 0f, Input.acceleration.x);
您仍然需要一种方法来检测您是为移动设备还是桌面设备构建。这可以通过 Unity 的预处理器 directives.
来完成void FixedUpdate()
{
Vector3 movement = Vector3.zero;
//Mobile Devices
#if UNITY_IOS || UNITY_ANDROID || UNITY_WSA_10_0
movement = new Vector3(-Input.acceleration.y, 0.0f, Input.acceleration.x);
#else
//Desktop
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
movement = new Vector3(moveHorizontal, 0f, moveVertical);
#endif
rb.AddForce(movement * speed);
}