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