Home>

Thanks for your attention.
This time I'm going to ask about the kinematic of Unity 2d.

Status

I am currently making a hit judgment with Unity2d, but it will be different from what I expected, and I will not come out even if I check it.

I want to move the object with the Collider and Rigidbody as shown below with a script, and when I hit another Collider, I don't want to go any further (I don't want to slip through), but it slips through.

The OnCollisionEnter2D function itself is called, but it is slipping through.

Question

In Rigidbody with kinematic setting, is OnCollisionEnter2D called, but is it correct behavior to slip through?
If so, is there a way to prevent it from slipping through?

  • Answer # 1

    According to the manual

      The

    Use Full Kinematic Contacts option enables the kinematic Rigidbody 2D to collide with all Rigidbody 2D body types. This is similar to dynamic Rigidbody 2D. However, when a kinematic Rigidbody 2D touches another Rigidbody 2D, it does not move with the physics engine, but it behaves like a non-moving object with infinite mass.

    It is

    , and it seems that the force to push back against indentation does not work. How about keeping Body Type Dynamic and using AddForce?

    using UnityEngine;
    [RequireComponent (typeof (Rigidbody2D))]
    public class ForceApplier: MonoBehaviour
    {
        [SerializeField] private float acceleration = 500.0f;
        [SerializeField] private float resistance = 100.0f;
        private new Rigidbody2D rigidbody2D;
        private void Start ()
        {
            this.rigidbody2D = this.GetComponent();
        }
        private void FixedUpdate ()
        {
            var fx = Input.GetAxisRaw ("Horizontal") * this.rigidbody2D.mass * this.acceleration;
            var rx = this.rigidbody2D.velocity.x * this.resistance;
            this.rigidbody2D.AddForce (new Vector2 (fx-rx, 0.0f));
        }
    }