Hey there. I have a ball and when it collide with another sprite, say a circle, i want ball to be centered inside the sprite and be static and all the ball’s velocity should be transferred to the sprite and then sprite should start moving with the same transferred velocity.
I am trying my best and so far. With the code provided below, ball is striking the sprite but ball doesn’t seem to be in center of the sprite and stay on its place where collision happened. Any help would be appreciated. Here is the link to the problem video.
private Rigidbody2D rb;
private Vector2 eggVelocity;
private Rigidbody2D eggRigidBody;
void Start()
{
rb = GetComponent<Rigidbody2D>();
rb.gravityScale = 0;
}
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Egg"))
{
eggRigidBody = collision.rigidbody;
eggVelocity = collision.rigidbody.velocity;
eggRigidBody.transform.position = transform.position;
eggRigidBody.bodyType = RigidbodyType2D.Static;
rb.gravityScale = 1.0f;
rb.velocity = eggVelocity;
}
}