【问题标题】:How do i modify only 1 dimension in Unity using c#?如何使用 c# 在 Unity 中仅修改 1 个维度?
【发布时间】:2020-05-07 15:05:20
【问题描述】:

我正在做一个简单的游戏来开始统一,有一个立方体可以直行并躲避随机产生的其他立方体(障碍物)。为了让障碍物始终在玩家面前产生,我设置了一个代码,使 7 个产生位置(随机生成的方块)跟随玩家的坐标,但在 Z 上加 100,这样它们就不会在玩家上方生成。现在,我的问题是,当生成障碍物时,它们也会改变它们的 X 位置,当我和玩家一起向右或向左移动时,它们会从平台上掉下来。我怎样才能让他们只跟随玩家的 Z 位置而不是 X? 下面是让生成器改变位置的代码:

public class MoveSpawn : MonoBehaviour
{

    public Transform player;
    public Vector3 offset;


    // Update is called once per frame
    void Update()
    {
        transform.position = player.position + offset;

    }
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    我只需将玩家的位置乘以 (0,0,1)(换句话说,Vector3.forward),然后再将其添加到偏移量:

    public class MoveSpawn : MonoBehaviour
    {
    
        public Transform player;
        public Vector3 offset;
    
    
        // Update is called once per frame
        void Update()
        {
            transform.position = player.position * Vector3.forward + offset;
    
        }
    }
    

    【讨论】:

      【解决方案2】:

      哦,这很容易。 因此,如果您只想查看 Cube 或 Unity 中任何对象的 Z(或任何其他)坐标,您可以简单地使用: transform.position.z 这与您对向量所做的相同。类似的东西:

      Vector3 offset = ...;
      Debug.Log(offset.x) //returns x coordinate of offset
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-07-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-30
        • 1970-01-01
        • 2013-08-02
        相关资源
        最近更新 更多