【问题标题】:c# unity2d: How can I move an Object and to change the direction when it collides?c# unity2d:如何移动对象并在碰撞时改变方向?
【发布时间】:2022-12-08 00:44:19
【问题描述】:

对象移动,但它不会改变它的方向,我不知道为什么。 我认为问题在于,但我不是 100% 确定。

Thats the code for the object named 'zackenblock':

And thats the code from the tilemap:

以下是一些截图:

Screenshot of unity and the object

Screenshot of unity and the tilemap

Screenshot of unity and the tilemap2

物体应该向右移动,当它发生碰撞时再向左移动。

它只会不停地向右移动。

【问题讨论】:

  • 嗨 - 你能稍微重新格式化你的问题吗,你应该使用代码块标记将代码包含在问题中,并删除注释掉的代码。查看*.com/help/how-to-ask了解更多信息
  • 你可能想看看物理材料。

标签: c# unity3d


【解决方案1】:

使用 OnCollisionEnter2D 因为你正在制作 2D 游戏

private void OnCollisionEnter2D(Collision2D col)
        {
           
        }

如果碰撞持续存在,还可以检查 OnCollisionStay2D/exit 。

【讨论】:

    【解决方案2】:

    您的代码的主要问题是您拼写错误。将“collosin”更改为“collision”。并改变:

    void OnCollisionEnter(Collision collision)
    

    进入

    void OnCollisionEnter2d(Collision2d collision)
    

    除此之外,您的代码似乎还不错。

    【讨论】: