Skip to main content
Don't repeat tags in title.
Source Link
DMGregory
  • 141k
  • 23
  • 258
  • 401

Calculate offset when attaching gameobjecta game object to edge of another gameobject [Unity2D]game object

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, follows it 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 spawnpointspawn point and assign that transform to the hooked gameobjectgame 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 gameobjects sizegame 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:

enter image description hereDiagram

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 gameobjectgame object doesn't clip my hook?

Calculate offset when attaching gameobject to edge of another gameobject [Unity2D]

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

Hook example

But the objects the hook can collide with are not the same sizes, so I can't just create a fixed spawnpoint and assign that transform to the hooked gameobject, 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 gameobjects size, 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:

enter image description here

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 gameobject doesn't clip my hook?

Calculate offset when attaching a game object to edge of another game object

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?

Source Link

Calculate offset when attaching gameobject to edge of another gameobject [Unity2D]

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

Hook example

But the objects the hook can collide with are not the same sizes, so I can't just create a fixed spawnpoint and assign that transform to the hooked gameobject, 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 gameobjects size, 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:

enter image description here

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 gameobject doesn't clip my hook?