- Home /
2D physics object acts weird when pressed into other physics object
The problem is in this video: https://youtu.be/7G9gqGObcb4
Setup: I have a simple game where you catch basketballs with a hoop that follows your mouse/touch input. The hoop is controlled via rb2d.MovePosition. The ball gets instantiated on top of the viewport and just falls down via standard physics. Also there are colliders situated at the edges of the screen.
Now when I try to push the hoop into the ball, the ball gets squeezed against the screenedge collider, but the hoop weirdly enough moves away from the ball. I want the squeezing to happen, but why is the hoop moving away?
Relevant stuff:
public class FollowMouse : MonoBehaviour
{
public bool FollowX = true;
public bool FollowY = false;
public bool FollowZ = false;
public float xMin = 0;
public float xMax = 0;
public float yMin = 0;
public float yMax = 0;
public float zMin = 0;
public float zMax = 0;
private Rigidbody2D rb2d;
// Start is called before the first frame update
void Start()
{
rb2d = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 position = transform.position;
float x = FollowX ? mousePos.x : position.x;
float y = FollowY ? mousePos.y : position.y;
float z = FollowZ ? mousePos.z : position.z;
if(FollowX)
x = Mathf.Clamp(x, xMin, xMax);
if(FollowY)
y = Mathf.Clamp(y, yMin, yMax);
if(FollowZ)
z = Mathf.Clamp(z, zMin, zMax);
Vector3 newPosition = new Vector3(x,y,z);
//Important: Use rigidbody MovePosition, otherwise physics get wonky
rb2d.MovePosition(newPosition);
}
}
Setup and Hoop in Inspector:
Ball in Inspector:
The bouncing ball physics material has friction of 0.4 and bounciness of 0.8.
Found out that it is because of the bounciness of the physics material. Is there any way I can go around that without removing the bounciness of the ball? I tried to make the edges of the hoop bouncy instead of the ball, but that lead to a host of other problems which made the physics even worse. Anyone any idea?