I had some questions about Physics / Rigidbody in Unity.
I have a 2D sprite with a collider. The 2D sprite is simply a card that I place on a table with the mouse. I use the collider for raycasting (so that
OnMouseDownis called on the card). In this case, should I change the card’s position via theTransformor via aRigidbody(which I don’t have right now)? I would say via a kinematicRigidbody, because anything that has a collider and moves should be controlled through theRigidbodyfor the colliders to work correctly (well, actually this is what I think, see question 2).Is it true that if I have a GameObject with a collider and the GameObject moves, then the GameObject must have a Rigidbody? (Moving the GameObject by changing the
Transformis bad for the collider?) I came across this post (please read it) and the staff user answering the post seems pretty sure about this (or maybe I understood nothing...).
So, this is my code when using the transform to change the position of the card (this works perfectly):
private void OnMouseDown()
{
_startPosition = transform.position;
_startDragVector = FromMouseToWorld();
}
private void OnMouseDrag()
{
Vector3 currentDragVector = FromMouseToWorld();
transform.position = (currentDragVector - _startDragVector) + _startPosition;
}
private Vector3 FromMouseToWorld()
{
Vector3 sp = Input.mousePosition;
sp.z = 0;
return _camera.ScreenToWorldPoint(sp);
}
Instead, this is my code when using the rigidbody to change the position of the card (this works perfectly, but slightly different due to the Lerp):
private void FixedUpdate()
{
Vector2 mov = Vector2.Lerp(_rb.position, _currentDragVector, Mathf.Clamp01(Time.fixedDeltaTime * speed));
_rb.MovePosition(mov);
}
//OnMouseDrag is called every frame, so it isn't like FixedUpdate, i.e. it's like Update
private void OnMouseDrag()
{
_currentDragVector = FromMouseToWorld();
}
private Vector3 FromMouseToWorld()
{
Vector3 sp = Input.mousePosition;
sp.z = 0;
return _camera.ScreenToWorldPoint(sp);
}
Which is the right approach? Thanks in advance :)

