I’m a bit confused on how to have one object push another object. To explain the situation I have linked to a gif below (for some reason I’m not allowed to upload gifs to this site)
The box in the gif is being moved by holding finger on it and moving it around the screen. The box is not stopped by anything and can be moved freely around. The ball should be pushed to the side when the box hits it, like shown in the gif.
In my game the balls are very small compared to the box, not the same scale as in the gif.
Currently I have tried to have the balls use raycasts to see if it’s near the box and they are hitting it correctly. Now how I get to use that information to push the balls away is a bit of a mystery to me.
My current code looks like this:
private bool HandleCollission(Vector2 rayDirection, float rayDistance, Color color)
{
Vector3 rayOrigin = transform.position;
Vector2 rayVector = new Vector2(rayOrigin.x, rayOrigin.y);
Debug.DrawRay(rayVector, rayDistance * rayDirection, color);
RaycastHit2D raycastHit = Physics2D.Raycast(rayVector, rayDirection, rayDistance, CollissionMask);
if (!raycastHit)
{
return false;
}
float diffX = raycastHit.point.x - rayVector.x;
float diffY = raycastHit.point.y - rayVector.y;
transform.position = new Vector3(diffX, diffY, transform.position.z);
return true;
}
The code is placed in the ball and it has a rigidbody2d attached to the gameobject
Anybody here who has some suggestions on how to do it?