Search Unity

does Unity Collision Detection Work if i am manually updating positions

Discussion in 'Physics' started by EliJNemer, Jun 29, 2020.

  1. EliJNemer

    EliJNemer

    Joined:
    Aug 3, 2019
    Posts:
    123
    hello, in my game i have written all my physics, but i am now at the collision detection part of the problem.

    my question is. if on my rigid bodies, the x y z positions and rotations are all clamped. and the physics is updated manually, can i still use the collision detection built into unity?

    i ask this because i was making my build i would shoot an object but it would not collide with the other object.. they all had colliders and rigidbodies.. but with respect to the physics engine of untiy they RB.Velocity = 0;
    becasue i have my own values of velocity and position seperate to unity..
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    Yes, you can do that:
    • Use Unity Rigidbodies marked as Is Kinematic.
    • Calculate the positions and rotations normally in your physics.
    • Update the position and rotation of the Unity Rigidodies with Rigidbody.MovePosition and Rigidbody.MoveRotation every FixedUpdate (not in Update).
    • Set Project Settings > Physics > Contact Pairs Mode to Enable All Contact Pairs.
    Now the methods OnCollisionEnter, OnCollisionStay, etc (and the OnTrigger equivalents on Trigger colliders) will report the collisions as when using Unity physics normally.

    Alternatively, you may use the method Physics.ComputePenetration and/or others in the Physics class to run quieres with the colliders in the Unity physic world.
     
    EliJNemer likes this.