gpt4 book ai didi

c# - 无法在精确点向上移动对象 - Unity

转载 作者:太空宇宙 更新时间:2023-11-03 16:40:50 24 4
gpt4 key购买 nike

我是unity新人。这是我想要实现的场景。

我有一个敌人和一座塔。敌人与塔相撞并移动到塔顶以逃离它。我无法在塔顶的确切位置移动敌人。

这是我的代码:

void OnTriggerEnter(Collider collider) {

moveY = tf.position.y + 6; //6 is the height of tower
tf.Translate(0, moveY, 0, Space.World);

}

如果我使用位置而不是平移,它会移动到塔顶位置,但我想将敌人带到塔顶。使用 translate 将敌人移动到底部。请帮我解决这个问题

最佳答案

问题很可能是您的代码无法识别 tf。试试这个:

void OnTriggerEnter(Collider collider) 
{

moveY = collider.position.y + 6; //6 is the height of tower
collider.Translate(0, moveY, 0, Space.World);

}

如果碰撞的是游戏对象 tf,则 tf 将成为碰撞体。您需要进行 if 条件检查以确保对撞机是 tf

关于c# - 无法在精确点向上移动对象 - Unity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7580611/

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