在Unity中以圆周运动控制/移动对象

时间:2015-06-08 12:26:35

标签: c# unity3d rotation position

基本上,我想要它所以我可以向左或向右移动一个物体,但是以圆周运动而不是直线运动。这是因为对象是另一个球体的子对象,我想使用左/右箭头键在球体周围移动对象,以便可以建立球体周围的位置。

我找到了一些只在一个方向上移动圆圈的代码,我无法控制它。这是:

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);
}

如果有人可以尝试转换"将此代码转换为我可以使用左右箭头键控制的代码,并使其左右移动,这将是很棒的。其他提交也非常感谢

2 个答案:

答案 0 :(得分:3)

以下是使用左右箭头键旋转的完整代码

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);
}  

答案 1 :(得分:2)

0K .......... .......... .......... .......... ..........   28.17 KB/s