Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以统一的圆周运动控制/移动对象_C#_Unity3d_Rotation_Position - Fatal编程技术网

C# 以统一的圆周运动控制/移动对象

C# 以统一的圆周运动控制/移动对象,c#,unity3d,rotation,position,C#,Unity3d,Rotation,Position,基本上,我想要它,这样我就可以向左或向右移动一个物体,但要做圆周运动,而不是直线运动。这是因为该对象是另一个球体的子对象,我希望使用左/右箭头键围绕球体移动该对象,以便可以确定球体周围的位置 我发现一些代码只在一个方向上移动圆,我无法控制它。这是: float timeCounter = 0; void Update () { timeCounter += Time.deltaTime; float x = Mathf.Cos (timeCounter);

基本上,我想要它,这样我就可以向左或向右移动一个物体,但要做圆周运动,而不是直线运动。这是因为该对象是另一个球体的子对象,我希望使用左/右箭头键围绕球体移动该对象,以便可以确定球体周围的位置

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

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

如果有人能尝试将此代码“转换”为我可以用左右箭头键控制的代码,并使其左右移动,那就太好了。其他提交的文件也非常感谢

这里有一个完整的代码,可以使用左右箭头键进行旋转

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

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

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