所以我已经完成了 Unity 的 Roll a ball 教程,这是一个小游戏,它使用了一个球体,其中应用了一个刚体,并带有一些基本的运动脚本。
我现在想要的是更进一步,并引入一个更高级的移动脚本,它也可以使用鼠标输入。
我想要实现的是根据局部轴添加力,所以如果我将鼠标向左移动,球会转动并且在那个方向上添加力。让我展示一下我想出的代码(添加到应用刚体的简单球体中):
using UnityEngine;
using System.Collections;
public class playerController : MonoBehaviour {
public float turnSpeed = 2.0f;
public float moveSpeed = 250.0f;
void FixedUpdate() {
float h = turnSpeed * Input.GetAxis("Mouse X");
transform.Rotate(0, h, 0);
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rigidbody.AddForce(movement * moveSpeed * Time.deltaTime);
}
}
好的,所以发生的事情是当我移动鼠标时球正在转动,如果我使用箭头键,球正在滚动,但是经过反复试验后我无法弄清楚的是让球移动它转向的方向。
您将如何处理这种特殊情况?任何帮助都是非常感谢的家伙。