15.1 C
New York
Friday, October 24, 2025

unity – When ought to an object with a collider have a rigidbody?


I had some questions on Physics / Rigidbody in Unity.

  1. I’ve a 2D sprite with a collider. The 2D sprite is solely a card that I place on a desk with the mouse. I take advantage of the collider for raycasting (in order that OnMouseDown is named on the cardboard). On this case, ought to I alter the cardboard’s place by way of the Remodel or by way of a Rigidbody (which I don’t have proper now)? I’d say by way of a kinematic Rigidbody, as a result of something that has a collider and strikes needs to be managed by way of the Rigidbody for the colliders to work accurately (effectively, really that is what I feel, see query 2).

  2. Is it true that if I’ve a GameObject with a collider and the GameObject strikes, then the GameObject will need to have a Rigidbody? (Shifting the GameObject by altering the Remodel is unhealthy for the collider?)
    I got here throughout this submit (please learn it) and the employees person answering the submit appears fairly positive about this (or perhaps I understood nothing…).

So, that is my code when utilizing the remodel to alter the place of the cardboard (this works completely):

    personal void OnMouseDown()
    {
        _startPosition = remodel.place;
        _startDragVector = FromMouseToWorld();
    }

    personal void OnMouseDrag()
    {
        Vector3 currentDragVector = FromMouseToWorld();
        remodel.place = (currentDragVector - _startDragVector) + _startPosition;
    }

    personal Vector3 FromMouseToWorld()
    {
        Vector3 sp = Enter.mousePosition;
        sp.z = 0;
        return _camera.ScreenToWorldPoint(sp);
    }

As an alternative, that is my code when utilizing the rigidbody to alter the place of the cardboard (this works completely, however barely completely different as a result of Lerp):

    personal void FixedUpdate()
    {
        Vector2 mov = Vector2.Lerp(_rb.place, _currentDragVector, Mathf.Clamp01(Time.fixedDeltaTime * pace));
        _rb.MovePosition(mov);
    }

    //OnMouseDrag is named each body, so it is not like FixedUpdate, i.e. it is like Replace
    personal void OnMouseDrag()
    { 
        _currentDragVector = FromMouseToWorld();
    }

    personal Vector3 FromMouseToWorld()
    {
        Vector3 sp = Enter.mousePosition;
        sp.z = 0;
        return _camera.ScreenToWorldPoint(sp);
    }

Which is the fitting strategy?
Thanks upfront 🙂

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles