简体   繁体   English

如何让一个游戏对象的 x 位置等于另一个对象的 x 位置? (2D Unity 游戏)

[英]How do I get one game objects x position to equal another objects x position? (2D Unity Game)

I have 2 game objects, one that is in a set X position and one that I was to spawn in the same x-position depending on.我有 2 个游戏对象,一个在设置的 X 位置,另一个在相同的 x 位置生成,具体取决于。 So Object1 has a set x position then object2 should find Object1's x-position to line it up properly.所以对象 1 有一个设置的 x 位置,然后对象 2 应该找到对象 1 的 x 位置以正确排列它。

Here is what I have tried, and it is not working:这是我尝试过的,但它不起作用:

 public GameObject LeftSpawn;
    public GameObject MiddleSpawn;
    public GameObject RightSpawn;
    public GameObject EnemyLeftSpawn;
    public GameObject EnemyMiddleSpawn;
    public GameObject EnemyRightSpawn;

    public GameObject LeftButton;
    public GameObject MiddleButton;
    public GameObject RightButton;

    private float pos1;

    // Start is called before the first frame update
    void Start()
    {
        pos1 = GameObject.Find("Left Button").transform.position.x;
        GameObject.Find("Left Enemy Spawn Point").transform.position.x = pos1;
    }

You can pass the first one to the second one.您可以将第一个传递给第二个。 then in the Update() method use the transform.Translate() to change it's position to the other ones.然后在Update()方法中使用transform.Translate()将其位置更改为其他位置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么使用 Instantiate 生成的游戏对象在游戏中不可见,但在我按统一播放时在场景中可见(2d 游戏) - why are the spawned game objects with Instantiate are invisible in game but visible in scence when i press play in unity (2d game) 当玩家 object 在一个统一的 3d 游戏中变得更大时,如何让相机移动 position? - How do I make the camera move position as the players object becomes bigger in a unity 3d game? 基于XNA Tile的游戏:使用2D数组中的网格位置检查2个对象之间的碰撞 - XNA Tile based Game: Checking collision between 2 objects using their grid position inside a 2D array 无法统一引用游戏对象的 2D 数组 - Unable to reference 2D array of game objects in unity Unity Animator 组件不必要地覆盖了游戏对象的脚本 position - Unity Animator component unnecessarily overriding scripted position of Game Objects 如何在 Unity 中启用和禁用多个游戏对象? - How do I enable and disable multiple game objects in Unity? 如何使用代码获取给定 position 处的对象列表? Unity3D - How to get list of objects at given position with code? Unity3D Unity 2D 相对于 Transform 改变对象位置 - Unity 2D change objects position relatively to a Transform Unity 2D(C#)-同一位置有更多对象 - Unity 2D (C#) - More objects in same position Unity如何从与另一个对象碰撞的对象获取位置? - Unity how to get position from objects that has collided with another object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM