简体   繁体   English

Unity强制物理引擎检查碰撞

[英]Unity forcing physics engine to check collisions

I am creating a game in unity where the player can drag one object (a card) over another (an enemy).我正在统一创建一个游戏,玩家可以将一个对象(一张卡片)拖到另一个对象(一个敌人)上。 If they drag the card over the enemy I want to run some code, and if not I want to return the card to its initial position.如果他们将卡片拖到敌人身上,我想运行一些代码,如果不是,我想将卡片返回到其初始位置。 I placed a collider on both objects but it isnt working the way it should.我在两个对象上都放置了一个对撞机,但它没有按应有的方式工作。 I am assuming this is because the object is getting moved back to its initial location before the physics engine sees the collision, but I don't know how to fix this.我假设这是因为对象在物理引擎看到碰撞之前被移回其初始位置,但我不知道如何解决这个问题。 I am deactivating the collider on the card while moving it to avoid having it trigger collisions until the player has placed it using the onmousedown and onmouseup events.我在移动卡片时停用了卡片上的对撞机,以避免它触发碰撞,直到玩家使用 onmousedown 和 onmouseup 事件放置它。 Any tips on how to fix this behavior?有关如何解决此行为的任何提示? can I force the physics engine to check collisions with the onmouseup event?我可以强制物理引擎检查与 onmouseup 事件的碰撞吗? I know the collisions are working because when I turn off the return to initial position behavior the game functions as expected.我知道碰撞正在起作用,因为当我关闭返回初始位置行为时,游戏会按预期运行。

How about useing the collider as trigger and do not use rigidbodys or anything.如何使用对撞机作为触发器而不使用刚体或任何东西。 Then if there is a trigger enter event set bool as true.然后,如果有触发器输入事件,则将 bool 设置为 true。 If there trigger exit reset the bool to false.如果有触发器退出,则将布尔值重置为 false。

Now if you "Release" the card check if bool is true or false.现在,如果您“释放”卡片,请检查 bool 是真还是假。

  • true: Set the cards position to the player or what you want true:将卡片位置设置为玩家或您想要的
  • false: Reset the cards position to the start false:将卡片位置重置为开始

Now beeing a little mroe fancy you can set a lighted border around the card when bool is active (just check in update)现在有点喜欢你可以在 bool 激活时在卡片周围设置一个发光的边框(只需签入更新)

Example:例子:

public class Card : MonoBehaviour {
    private bool isHolding;
    private bool isHovering;

    public Vector3 startPos;

    public void Start() {
        startPos = transform.position;
    }
    
    public void Update() {
        // Code where you check if the card is Clicked and Moved by the player
        // If so set isHolding = true
        
        // dont enter the check if holding blablabla when animation stuff is happening
        if (doAnimationStuff) {
            // do animation
            // Destroy Object
            return;
        }
        
        // Code to check if isHolding == false
        if (!isHolding) {
            if (!isHovering) {
                transform.position = startPos;
            } else {
                doAnimationStuff = true;
            }
        }
    }

    private void OnTriggerEnter(Collider other) {
        // Check if other is a player
        // if so set isHovering = true
    }

    private void OnTriggerExit(Collider other) {
        // Check if other is a player
        // if so set isHovering = false
    }
}

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

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