gpt4 book ai didi

c# - 统一: One unit hitting another - C#

转载 作者:太空宇宙 更新时间:2023-11-03 23:04:21 25 4
gpt4 key购买 nike

我是 unit 的新手,我正在制作这款砍杀游戏。不过,我无法让一个单位对另一个单位造成伤害编辑:我忘了提到动画师正在调用 hit() 方法。

public GameObject obj;

(...)

void OnTriggerEnter2D(Collider2D collider)
{
if(!collider.gameObject.GetComponent<floor>())
{
obj=collider.gameObject;
print(obj);
}
}


void hit()
{
print(obj);
if(kicked&&obj)
{
obj.GetComponent<health>().DealDamage(damage);
print(obj.GetComponent<health>().value);
}
}

动画正在调用 hit() 方法(当执行踢动画时)。顺便说一句,当你踢球时,踢球的 bool 值变为真。 hit 方法每次都打印 (null)。

最佳答案

改变

GameObject obj=collider.gameObject;

obj=collider.gameObject;

您在 OnTriggerEnter2D 方法中声明了第二个“obj”变量,它是该方法的局部变量,它覆盖了您在类中声明的变量。我可能应该提到您的代码(虽然它可能有效)写得非常糟糕。您应该阅读 OO 设计模式。

关于c# - 统一: One unit hitting another - C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41858060/

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