I’m trying to replicate the operation of the mechanics of the superliminal game, and so far the scaling works correctly, the only problem is that when I relocate the grabbed object it overlaps with what is placed, for this I read that I could use Physics.OverlapBox, but after the second execution of the update, it recalculates in loop the position and the scale, could you give me advice on how I can avoid it?
private void Grab()
{
RaycastHit hit;
if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, Mathf.Infinity, grabbableLayer))
{
if (hit.collider.CompareTag("Grabbable"))
{
grabbedGameObject = hit.collider.gameObject;
Rigidbody rb = grabbedGameObject.GetComponent<Rigidbody>();
Collider col = grabbedGameObject.GetComponent<Collider>();
if (rb != null) rb.isKinematic = true;
if (col != null) Physics.IgnoreCollision(playerCollider, col, true);
grabbedGameObject.transform.SetParent(holdPosition);
// Save originals
originalDistance = Vector3.Distance(Camera.main.transform.position, grabbedGameObject.transform.position);
originalScale = grabbedGameObject.transform.localScale;
Debug.Log($"Objeto agarrado: {grabbedGameObject.name}, Distancia Original: {originalDistance}");
isHoldingObject = true;
}
}
}
private void UpdateObjectPosition()
{
if (grabbedGameObject == null) return;
RaycastHit hit;
if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, Mathf.Infinity, resizableLayer))
{
float hitDistance = Vector3.Distance(Camera.main.transform.position, hit.point);
repositionedDistance = hitDistance;
Vector3 newScale = originalScale * (repositionedDistance / originalDistance);
if (newScale.magnitude >= 10f) newScale = Vector3.one * 10f;
Debug.Log($"Distancia Hit: {hitDistance}, Distancia Reposicionada: {repositionedDistance}, Nueva Escala: {newScale}");
if (repositionedDistance <= 3.0f)
{
Debug.Log("Distancia muy cercana, deteniendo ajuste");
return;
}
grabbedGameObject.transform.position = hit.point;
grabbedGameObject.transform.localScale = newScale;
}
}