Controlling/moving Unity 中做圆周运动的物体
Controlling/moving an object in a circular motion in Unity
基本上,我想要它以便我可以向左或向右移动对象,但是以圆周运动而不是直线运动。这是因为该对象是另一个球体的子对象,我想使用 left/right 箭头键围绕球体移动对象,以便可以建立围绕球体的位置。
我发现一些代码只能在一个方向上移动圆圈,但我无法控制它。这是:
float timeCounter = 0;
void Update () {
timeCounter += Time.deltaTime;
float x = Mathf.Cos (timeCounter);
float y = Mathf.Sin (timeCounter);
float z = 0;
transform.position = new Vector3 (x, y, z);
}
如果有人可以尝试 "convert" 将此代码转换为我可以使用左右箭头键控制的代码并使其左右移动,那就太好了。其他提交也非常感谢
float timeCounter = 0;
void Update () {
timeCounter += Input.GetAxis("Horizontal") * Time.deltaTime; // multiply all this with some speed variable (* speed);
float x = Mathf.Cos (timeCounter);
float y = Mathf.Sin (timeCounter);
float z = 0;
transform.position = new Vector3 (x, y, z);
}
这里是左右方向键旋转的完整代码
float timeCounter = 0;
bool Direction = false;
void Update () {
if(Input.GetKeyDown(KeyCode.LeftArrow))
{
Direction = false;
}
if(Input.GetKeyDown(KeyCode.RightArrow))
{
Direction = true;
}
if (Direction)
timeCounter += Time.deltaTime;
else
timeCounter -= Time.deltaTime;
float x = Mathf.Cos (timeCounter);
float y = Mathf.Sin (timeCounter);
float z = 0;
transform.position = new Vector3 (x, y, z);
}
基本上,我想要它以便我可以向左或向右移动对象,但是以圆周运动而不是直线运动。这是因为该对象是另一个球体的子对象,我想使用 left/right 箭头键围绕球体移动对象,以便可以建立围绕球体的位置。
我发现一些代码只能在一个方向上移动圆圈,但我无法控制它。这是:
float timeCounter = 0;
void Update () {
timeCounter += Time.deltaTime;
float x = Mathf.Cos (timeCounter);
float y = Mathf.Sin (timeCounter);
float z = 0;
transform.position = new Vector3 (x, y, z);
}
如果有人可以尝试 "convert" 将此代码转换为我可以使用左右箭头键控制的代码并使其左右移动,那就太好了。其他提交也非常感谢
float timeCounter = 0;
void Update () {
timeCounter += Input.GetAxis("Horizontal") * Time.deltaTime; // multiply all this with some speed variable (* speed);
float x = Mathf.Cos (timeCounter);
float y = Mathf.Sin (timeCounter);
float z = 0;
transform.position = new Vector3 (x, y, z);
}
这里是左右方向键旋转的完整代码
float timeCounter = 0;
bool Direction = false;
void Update () {
if(Input.GetKeyDown(KeyCode.LeftArrow))
{
Direction = false;
}
if(Input.GetKeyDown(KeyCode.RightArrow))
{
Direction = true;
}
if (Direction)
timeCounter += Time.deltaTime;
else
timeCounter -= Time.deltaTime;
float x = Mathf.Cos (timeCounter);
float y = Mathf.Sin (timeCounter);
float z = 0;
transform.position = new Vector3 (x, y, z);
}