简体   繁体   English

OnCollisionEnter在Unity3D中无法正常工作

[英]OnCollisionEnter doesn't work correct in Unity3D

I try to make a copy of the impossible game: http://flukedude.com/theimpossiblegame/ 我尝试制作不可能的游戏的副本: http : //flukedude.com/theimpossiblegame/

Red square are move in the right direction and when player see blue square, he should jump. 红色方块朝正确的方向移动,当玩家看到蓝色方块时,他应该跳跃。 If he will collide with the left side of the square (which is another square " WallCollider "), the game is over. 如果他将与正方形(另一个正方形“ WallCollider ”)的左侧碰撞,则游戏结束。 It works, but if player will jump on the upper left corner, the game will be over too. 它可以工作,但是如果玩家跳到左上角,游戏也将结束。 Can't understand, why. 不明白,为什么。

void OnCollisionEnter(Collision collision)
{
    if (collision.transform.name == "WallCollider")
        Debug.Log ("You lose");
}

在此处输入图片说明

Red and blue squares and WallCollider have box colliders. 红色和蓝色方块以及WallCollider具有框碰撞器。 Only red square have a rigidbody. 只有红色正方形具有刚体。

I move red like this: 我这样移动红色:

public GameObject player;

    public float speed = 5.0f;
    public float jumpPower = 10.0f;

    void Start ()
    {
        player = (GameObject)this.gameObject;
    }

    void Update ()
    {
        player.transform.position +=
        player.transform.right * speed * Time.deltaTime;
    }

I'm using 3d physics in a 2d environment. 我在2D环境中使用3D物理。 All scripts is located on the red square (player). 所有脚本均位于红色方块(播放器)上。

Blue box have one box collider. 蓝盒子有一个盒子对撞机。 But near it I have WallCollider (Cube too), which have a box collider too, but it's height a little bit lower. 但是在它附近,我有WallCollider(也有Cube),也有一个盒子对撞机,但是它的高度略低。

First of all, colliders have penetration distance so by jumping on upper left corner player collider may penetrate it down to the wall collider. 首先,对撞机具有穿透距离,因此,通过跳到左上角的玩家对撞机,可以将其向下穿透到墙壁对撞机。 Try to reduce penetration distance in Edit/Project settings/Physics . 在“ 编辑/项目设置/物理”中尝试减小穿透距离。

Also it will be helpful to decrease fixed timestep to improve accuracy of physics simulation (look for Edit/Project settings/Time ). 这也将有助于减少固定的时间步长,以提高物理模拟的准确性(查找“ 编辑/项目设置/时间” )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM