控制/以圆周运动在Unity移动物体

问题描述 投票:1回答:2

基本上,我想,所以我可以向左或向右移动的物体,但在一个圆周运动,而不是直线。这是因为对象是另一个球的孩子,我想用左边移动的物体周围的球体/右arrowkey所以球体周围的位置可以成立。

我发现一些代码,移动一圈只在一个方向,我无法控制它。这里是:

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

如果有人可以尝试“转换”这个代码放到一个代码,我可以用左,右arrowkey控制并使其移动左,右,这将是巨大的。其他意见也非常感谢

c# unity3d rotation position
2个回答
2
投票
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);
}

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);
}  
© www.soinside.com 2019 - 2024. All rights reserved.