如何制作一个游戏对象仅在 x 轴上跟随另一个游戏对象?

How to make a gameObject Follow another game object on the x axis only?

我想弄清楚如何让这个游戏对象仅在 x 轴上跟随另一个游戏对象。这是我目前所有的。

public Transform mouse; 
private Vector3 offset; 

// Start is called before the first frame update
void Start()
{
    
}

// Update is called once per frame
void Update()
{
    
}


void follow()
{
    transform.position.x = mouse.position.x;
}

你几乎成功了!我可能解释得不好,但不起作用的原因是因为 vector3 具有只读属性,要更改它,您必须创建一个新属性,所以可能有更好的方法,但这是我一直使用的方法。

new Vector3(mouse.x, transform.position.y, transform.position.z)

干杯