gpt4 book ai didi

C# - 我如何让 2 个游戏对象在统一中始终具有相同的 y 值

转载 作者:行者123 更新时间:2023-11-30 14:22:23 25 4
gpt4 key购买 nike

我目前在我的场景中有一个立方体,我已经四处移动了。我希望另一个立方体始终具有相同的 y 值。因此,如果第一个立方体向下移动 10 个单位,我希望另一个立方体也这样做。

我的第一个立方体是在编辑器中手动创建的,但我的另一个立方体是使用脚本放置的。谢谢!

最佳答案

如前所述,您可以使用父子关系,但是父子关系的每一次移动都会导致子对象在 x、y 和 z 坐标上发生移动。

如果您希望其他对象遵循 y 坐标而不是其他对象,那么您不能为此使用父子关系。

相反,您可以使用脚本(灵感来自:https://answers.unity.com/questions/543461/object-follow-another-object-on-the-x-axis.html)

using UnityEngine;
using System.Collections;

public class SameYCoordinateAsOther : MonoBehaviour {

Transform otherTransform;

void Start() {
// you can set a reference to the "parent" cube
otherTransform = GameObject.Find("cube1").transform;
}

void Update() {
// here we force the position of the current object to have the same y as the parent
transform.position = new Vector3(transform.position.x, otherTransform.position.y, transform.position.z);
}
}

您只需将此脚本附加到任何必须“跟随”y 轴上第一个立方体的对象。

此脚本将强制第二个对象具有与第一个对象相同的 y 值。

如果您希望它们具有相同的值,但只是移动量相同,这会有点复杂。

关于C# - 我如何让 2 个游戏对象在统一中始终具有相同的 y 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49848372/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com