0
\$\begingroup\$

I am doing a sort of hook, where whenever the hook collides with something, it retracts itself and pulls the the object it collided with back along with it.

Hook example

But the objects the hook can collide with are not the same sizes, so I can't just create a fixed spawn point and assign that transform to the hooked game object, as some of them would clip the hook and others would be too far away.

So I need to find out what the offset should be, based on the size of the hooked game objects, but I don't know how to calculate it. Especially since the hook can also be rotated, so just offsetting on the y axis for example is not enough, since it might also need to be offset on the x-axis - Pic explaining it:

Diagram

So any advice on how to calculate the offset (I have my bounds.size to use), and then apply it correctly on both the X and Y axis so the attached game object doesn't clip my hook?

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

So I finally figured out how to calculate the correct offset on both the x and y axis. It was due to my lack of knowledge on vectors..

I had to find the magnitude of the hooked game object and then multiply that by half (so I have half the size of the gameobject) and store that in a var called offsetDistance.

Then I multiply my position with -offsetDistance and then it works. Code used to solve it:

        float offsetDistance = spriteRenderer.bounds.size.magnitude * .5f;

        Vector3 hookPos = hook.transform.position;
        Vector3 hookDirection = hook.transform.up;

        Vector3 spawnPos = hookPos + hookDirection * -offsetDistance;
        transform.position = new Vector2(spawnPos.x, spawnPos.y);
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .