【问题标题】:how to only transform.position the x position and not also the y c# unity如何只transform.position x位置而不是y c# unity
【发布时间】:2020-08-05 05:01:48
【问题描述】:

我需要帮助来更改我的一个 c# 脚本。我已经创建了统一的 2d 精灵按钮​​,它们有一个盒子碰撞器并使用 OnMouseDown() 函数。我有一个 c# 脚本来使按钮跟随播放器并偏移它们,使它们不在播放器的顶部。但是当我的玩家跳跃时,按钮的 y 和 x 位置也会发生变化,因此按钮也会“跳跃”。我需要保留 coords.y = playerTransform.position.y;和偏移,因为按钮需要偏移。我希望按钮的位置在 x 轴上改变,而不是在 y 轴上。

这是我的代码:

Vector3 coords = transform.position; //stores the current camera position in coords

coords.x = playerTransform.position.x; // set coords to be equal to the current players x coordinate
coords.y = playerTransform.position.y;
coords.x += offsetX;
coords.y += offsetY;
transform.position = coords; //set the cameras position to be equal to coords

【问题讨论】:

  • 省略更改 y ... 的行?
  • 我同意 derHugo。将 coord.y 更改为等于某个常数。或者 delete 或者 lerp 让它在玩家中轻微反弹。

标签: c# unity3d


【解决方案1】:
transform.Translate(new Vector3(playerTransform.x + offsetX, 0, 0));

这需要更少的代码并将您传入的 Vector3 添加到 transform.position。只需将不想更改的坐标设置为0即可。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多