I had some questions on Physics / Rigidbody in Unity.
-
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
OnMouseDownis named on the cardboard). On this case, ought to I alter the cardboard’s place by way of theRemodelor by way of aRigidbody(which I don’t have proper now)? I’d say by way of a kinematicRigidbody, as a result of something that has a collider and strikes needs to be managed by way of theRigidbodyfor the colliders to work accurately (effectively, really that is what I feel, see query 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
Remodelis 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 🙂
