【问题标题】:Controlling/moving an object in a circular motion in Unity在 Unity 中以圆周运动控制/移动对象
【发布时间】:2015-06-08 12:26:35
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: c# unity3d rotation position


    【解决方案1】:

    这是一个完整的左右方向键旋转代码

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

    【讨论】:

      【解决方案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);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-06
        • 1970-01-01
        相关资源
        最近更新 更多