Search Unity

Triggers only working when game is Maximized on Play

Discussion in 'Editor & General Support' started by chapmanshane23, Apr 9, 2022.

  1. chapmanshane23

    chapmanshane23

    Joined:
    Nov 12, 2020
    Posts:
    7
    This is quite weird, not sure if its a bug or something I'm doing wrong.

    My onTriggerEnter and Exit2D don't work when I test my game in the smaller game view, only when I "Maximize On Play"

    Has anyone else run into an issue like this and perhaps have a fix?

    Thanks
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Are you using physics correctly? Sounds like you might not be.

    With Physics (or Physics2D), never manipulate the Transform directly. If you manipulate the Transform directly, you are bypassing the physics system and you can reasonably expect glitching and missed collisions and other physics mayhem.

    Always use the .MovePosition() and .MoveRotation() methods on the Rigidbody (or Rigidbody2D) instance in order to move or rotate things. Doing this keeps the physics system informed about what is going on.

    https://forum.unity.com/threads/col...-unity-physic-rigidbody.1216875/#post-7763061

    With a wheel collider, use the motor or brake torque to effect motion.
     
    chapmanshane23 and PraetorBlue like this.
  3. chapmanshane23

    chapmanshane23

    Joined:
    Nov 12, 2020
    Posts:
    7
    Thanks Kurt, I think that may have fixed it, although i'll have to rewrite most of my code (but thats okay!)

    another quick question

    What about with setting a parent?

    can i say:

    item.transform.SetParent(itemAnchor.transform);

    or will this mess with the physics as well? Im also changing the rigidbody to kinematic when doing this.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    It's not going to keep that Rigidbody in place relative to the parent, if that's what you are expecting.

    If you are trying to connect a Rigidbody to that parent, you need some kind of joint, such as a FixedJoint
     
  5. chapmanshane23

    chapmanshane23

    Joined:
    Nov 12, 2020
    Posts:
    7
    im using a raycast to see if the player is within reach to an item
    Then on a button press, moving that item to the players hand.
    so the item will move with the player.

    if(player.itemPicked){
    item.transform.SetParent(itemAnchor.transform);
    item.GetComponent<Rigidbody2D>().MovePosition(itemAnchor.transform.position);
    item.GetComponent<Rigidbody2D>().MoveRotation(itemAnchor.transform.rotation);
    item.GetComponent<Rigidbody2D>().isKinematic = true;
    }

    are you saying it would be better for me to attach a fixedjoint to the itemAnchor and then, on button press, attach the item to that fixed joint?
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Interesting... I guess it varies by what you're trying to do. Never thought of parenting it AND driving position rotation... does it work? If so well, I suppose move on! :)
     
  7. chapmanshane23

    chapmanshane23

    Joined:
    Nov 12, 2020
    Posts:
    7
    yeah it works :) thanks