简体   繁体   English

自定义物理问题中的碰撞

[英]Collisions in custom physics problems

I'm kind of new to Unity.我对 Unity 有点陌生。 As by the title, I am having trouble getting the collisions right in my game.正如标题一样,我在游戏中无法正确处理碰撞。 I am using the custom physics script from unity: https://unity3d.com/learn/tutorials/topics/2d-game-creation/scripting-gravity?playlist=17093 .我正在使用统一的自定义物理脚本: https : //unity3d.com/learn/tutorials/topics/2d-game-creation/scripting-gravity?playlist=17093 In my game, I am experiencing difficulties in disable collisions.在我的游戏中,我在禁用碰撞方面遇到了困难。

For example, when I use例如,当我使用

Physics2D.IgnoreLayerCollision (8, 9);

It doesn't change anything and the two characters still collide.它没有改变任何东西,两个角色仍然碰撞。 Also, for some reasons, the triggers behave strange and are still affected by collisions.此外,由于某些原因,触发器的行为很奇怪并且仍然受到碰撞的影响。 Going near a character with triggers will make him float up.靠近带有触发器的角色会使他漂浮起来。 I've been stuck on this for a long time and I'd really appreciate the help.我已经被困在这个问题上很长时间了,我真的很感激你的帮助。

It is worth noting that I am using custom physics and using a box collider 2d to detect attack ranges etc. Here is some code of that:值得注意的是,我正在使用自定义物理并使用框对撞机 2d 来检测攻击范围等。这是其中的一些代码:

Collider2D attackbox = Physics2D.OverlapBox (attackPos.position, new Vector2 (attackRangeX, attackRangeY), 0, whatIsPlayer); 
if (attackbox != null && !isDead && characterController.isDead == false) {
    targetVelocity = Vector2.zero;
    animator.SetTrigger ("isPunching");
    timeBtwAtk = startTimeBtwAtk;
}

and I have individual punch triggers in the animation to detect when the character is actually being hit:并且我在动画中有单独的打孔触发器来检测角色实际被击中的时间:

public void SetColliderIndex(int spriteNum)
{
    colliders[currentColliderIndex].enabled = false;
    currentColliderIndex = spriteNum;
    colliders[currentColliderIndex].enabled = true;
}

public void ClearColliderIndex()
{
    colliders[currentColliderIndex].enabled = false;
}

void OnTriggerEnter2D(Collider2D col)
{
    if (col.tag == "Player") 
    {
        col.GetComponent<CharacterController2D> ().TakeDamage (enemyDamage);
        col.GetComponent<CharacterController2D> ().canWait = false;
        col.GetComponent<CharacterController2D> ().canControl = false;
    }
}

When I use Physics2D.IgnoreLayerCollision (8, 9);当我使用Physics2D.IgnoreLayerCollision (8, 9); I want both specified layers not to interact whatsoever.我希望两个指定的层都不要进行任何交互。 I don't want any weird shifts, or floating when they pass through each other.我不想要任何奇怪的转变,或者当它们相互穿过时漂浮。 Also when I move my player agains the enemy, I don't want him to be able to push him back.此外,当我将我的玩家再次移动到敌人时,我不希望他能够将他推回去。 I want it to be as if he is running into a wall.我希望它就像他撞墙一样。

I have answered something similar to this here .在这里回答了类似的问题 Also, if you don't want the player suddenly floating upon collision, you can just set the rigidbody/rigidbody2d type to kinematic, but this mean the object will not be affected by physics.此外,如果您不希望玩家在碰撞时突然漂浮,您可以将刚体/刚体 2d 类型设置为运动学,但这意味着对象将不受物理影响。 you will have to do that in code.你必须在代码中做到这一点。

我认为您的问题是在某些时候两个对撞机相互作用和/或两者之一被激活,但是从您发布的代码中,如果有任何问题,我无法清楚地确定问题

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

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